Search code examples
javascriptangularjsng-options

ng-options filter equal to ng-model from input


I'm trying to add conditional filter to my ng-options. The options should only be displayed when an id from the options array equals an id from a different select input.

The first ng-options:

<select ng-model="requestDepartment" ng-options="department.DepartmentID as department.DepartmentName for department in departments" class="form-control">
    <option value="">Select</option>
</select>

The second ng-options with filtering:

<select ng-model="requestCategory" ng-options="category.CategorytName for category in categories | filter:{category.ParentID : requestDepartment}" class="form-control">
    <option value="">Select</option>
</select>

The second ng-options should only show entries which match the ng-model from the first ng-options.

The structure for the data is as follows:

DepartmentID: 1
DepartmentName: "IT"

ParentID: 1
CategoryName: "Sharepoint"

So, if department "IT" is selected I only want to display categories that match the parentID, in this case "Sharepoint".

I have tried filter:{category.ParentID : requestDepartment} without luck.

Any suggestions?

Update:

I have added a fiddle: http://jsfiddle.net/q53ro5sr/4/


Solution

  • This should work

    <select ng-model="requestCategory" 
            ng-options="category.CategoryName for category in categories | filter: { ParentID: requestDepartment }" 
            class="form-control">
          <option value="">Select</option>
    </select>
    

    Here is a Fiddle