How do I filter an array with AngularJS and use a property of the filtered object as the ng-model attribute?

前端 未结 8 1440
刺人心
刺人心 2020-11-30 19:17

If I have an array of objects, and I want to bind the Angular model to a property of one of the elements based on a filter, how do I do that? I can explain better with a con

8条回答
  •  一个人的身影
    2020-11-30 19:21

    You can use the "filter" filter in your controller to get all the "C" grades. Getting the first element of the result array will give you the title of the subject that has grade "C".

    $scope.gradeC = $filter('filter')($scope.results.subjects, {grade: 'C'})[0];
    

    http://jsbin.com/ewitun/1/edit

    The same with plain ES6:

    $scope.gradeC = $scope.results.subjects.filter((subject) => subject.grade === 'C')[0]
    

提交回复
热议问题