简体   繁体   中英

Can not fetch the value from array using Angular.js

I need one help.I am unable to fetch some value as per some condition using angular.js.I am explaining my code below.

 var subcategories = [{
            id:1,
            name: 'SubCategory 1',
            value: 1
          }, {
            id:2,
            name: 'SubCategory 2',
            value: 2
          }, {
            id:3,
            name: 'SubCategory 3',
            value: 3
          }, {
            id:4,
            name: 'SubCategory 4',
            value: 4
          }];
    var result = $filter('filter')(subcategories, {id:1})[0];
    console.log('result',result);

Here i need to fetch all data whose id=1 from subcategories object. But in console i am getting result undefined . Please help me.

Code works. Did you import the $filter directive into your controller??

https://jsfiddle.net/naLqezvs/

function Controller($scope,$filter) {
  $scope.subcategories = [{
        id:1,
        name: 'SubCategory 1',
        value: 1
      }, {
        id:2,
        name: 'SubCategory 2',
        value: 2
      }, {
        id:3,
        name: 'SubCategory 3',
        value: 3
      }, {
        id:4,
        name: 'SubCategory 4',
        value: 4
      }];
  $scope.results = $filter('filter')($scope.subcategories, {id:1});
  $scope.result = $scope.results[0];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM