0

I have an array on my controller

$scope.arr = [......], a

nd in html I want to do

ng-repeat = "item in arr | color:'blue'" //this line works, filter done in the app.filter way.

where color is an attribute of all objects in arr.

How Do I make this color:'blue' customizable and sub in color:'red'?

also if I want to do controller filtering instead of html, what would be the syntax, right now I have

$scope.filteredArr = $filter('color')($scope.arr,'blue'); 

which is giving error

http://jsfiddle.net/x3azn/YpMKX/

posted fiddle, please remove -1

3
  • 1
    Could you share some live code (in plunker or jsFiddle). There is probably a very simple answer to your question but it is hard to help based on your description and limited amount of code. Commented Apr 27, 2013 at 18:06
  • "which is giving error". Why didn't you post the error here? This really helps. Commented Apr 27, 2013 at 18:07
  • thank you i am sure that its simple, I just cant seem to find my solution on ng org Commented Apr 28, 2013 at 3:49

1 Answer 1

2

You can customize color:'blue' with any expression in the format filter:expression, so something like color:myColor would work fine provided a color filter has been defined and myColor exists in the current scope.

In your controller, you can do the same.

$scope.filteredArr = $filter('color')($scope.arr,myColor);

Here is an example based on your jsFiddle example.

Javascript:

angular.module('app', [])
    .filter('eyecolor', function () {
        return function ( people, color ) {
            var arr = [];
            for ( var i = people.length; i--; ) {
                if ( people[i].eyeColor === color ) {
                    arr.push( people[i] );
                }
            }
            return arr;
        }
    })
    .controller('ctrl', function ($scope, $filter) {
        $scope.desiredColor = 'Brown';
        $scope.people = [
            {       
                name: 'Bob',
                eyeColor: 'Brown'
            }, {
                name: 'Katherine',
                eyeColor: 'Yellow'
            }, {
                name: 'Chun',
                eyeColor: 'Black'
            }
        ];
        $scope.peopleFilter = $filter('eyecolor')( $scope.people, $scope.desiredColor );
    });

Html:

<div ng-app="app">
    <div ng-controller="ctrl">Color Desired:
        <input ng-model="desiredColor" /><br/>        
        <div ng-repeat="person in people | eyecolor: desiredColor">
            HTML filter: {{person.name}} has eye color {{person.eyeColor}}</div>
        <div ng-repeat="person in peopleFilter">
            Controller filter: {{ person.name }} has eye color {{ person.eyeColor }}</div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.