4

I have a select where I am populating certain values as below:

The values are:

     $scope.obj = {codes: [
        {code: 1},
        {code: 2},
        {code: 3}
     ]};

Now I want to filter certain values dynamically without making any changes in the $scope.obj value i.e. I don't want to display the {code:1} value. I have set this value in another $scope variable:

$scope.selectedCode = {code:1};

Whatever values is present in selected code should not be displayed in the select drop down. I think this can be done using angular js filter expression:

<select ng-options="c.code for c in obj.codes | filter:'c.code !== selectedCode .code'" ng-model="selected">

But the above is displaying an empty drop down.

Any help/guidance is most welcome.Thanks in advance. :)

1
  • There is a typo in your code snippet. You have 'filer' instead of 'filter'. Is that your actual snippet? Commented Jan 4, 2015 at 6:51

2 Answers 2

7

Create helper method in controller:

$scope.filterCodes = function(obj) {
    return obj.code !== $scope.selectedCode.code;
};

and use it as custom filter:

<select ng-options="c.code for c in obj.codes | filter:filterCodes" ng-model="selected"></select>

UPD. There is also pure template solution without controller function:

ng-options="c.code for c in obj.codes | filter: '!' + selectedCode.code"
Sign up to request clarification or add additional context in comments.

4 Comments

Worked like charm. Thanks a million. :)
In template you can do it like this: ng-options="c.code for c in obj.codes | filter: '!' + selectedCode.code". But personally I find it more comprehensive to have filtering in controller and keep templates as simple as possible.
I looked everywhere for something like this !! thanks a million !!
You've just saved my day
0

Tyr to create a new object which filtered.

<select ng-options="c.code for c in filteredobj = (obj.codes | filter:'code':'!'+ selectedCode.code)" ng-model="selected">

1 Comment

Sorry but your answer ain't working. In fact the select is not displaying any values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.