0

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;          
        }
 }

});

3 Answers 3

1

$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.

Sign up to request clarification or add additional context in comments.

1 Comment

However, you'll want an ng-show for the "this item is special" dialog, otherwise it only shows for items that are NOT special
1

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>

Comments

-1

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

2 Comments

It is supposed to be dynamic, special id can change
@user1477955 - why are you down voting an answer when your question did not state that? what is the criteria to hide the tag

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.