user1477955
user1477955

Reputation: 1670

Angular JS - Check a single value with a filter

My angularJS application displays the items of an object. If a single item has a certain ID I want to display a message. At the moment it does not work, what is wrong?

js fiddle

HTML

    <div data-ng-controller="myCtrl">


    <ul >
        <li data-ng-repeat="item in values"> 
            Item with id:<code>#{{item.id}}</code> 

            <code ng-hide="special(item.id)"> -> This id is special</code> 
        </li>
    </ul>

  </div>

NG

var app = angular.module('m', []);

app.controller('myCtrl', function ($scope) {
$scope.values = [{
    id: 1
}, {
    id: 2
 .....
}];

$scope.filter = [4,5,6];

$scope.filterIds = function (ids) {
        return function (item) {
            var filter = $scope.filter;

                 return filter.indexOf(item.id) !== -1;         
        }
 }

$scope.special = function (id) {
        return function (id) {
            var filter = $scope.filter;
                 return filter.indexOf(id) !== -1;          
        }
 }

});

Upvotes: 0

Views: 391

Answers (3)

sylwester
sylwester

Reputation: 16498

please see here http://jsfiddle.net/1rhvuyL1/

 $scope.special = function (id) {
        if ($scope.filter.indexOf(id) >=0)
        {return true;}

    }

HTML:

<div>
    <div data-ng-controller="myCtrl">
        <ul>
            <li data-ng-repeat="item in values">Item with id:<code>#{{item.id}}</code>  <code ng-show="special(item.id)"> -> This id is special</code> 
            </li>
        </ul>
    </div>
</div>

Upvotes: 1

Tom A
Tom A

Reputation: 964

$scope.special() returns a function inside of itself, so the return value is a function and not a boolean value. Replace it with this:

$scope.special = function (id) {
  var filter = $scope.filter;
  return filter.indexOf(id) !== -1;         
}

and you'll see that it works.

Upvotes: 1

Oam Psy
Oam Psy

Reputation: 8663

You dont need to create the special function inside the JS.

Try:

<div data-ng-controller="myCtrl">
    <ul >
        <li data-ng-repeat="item in values"> 
            Item with id:<code>#{{item.id}}</code> 
            <code ng-hide="item.id == '1'"> -> This id is special</code> 
        </li>
    </ul>
</div>

The above will hide the "This id is special" when id = 1

Upvotes: -1

Related Questions