Angular只从数组中获取所需的数据

时间:2016-05-19 12:46:43

标签: javascript angularjs

我有一个ng-repeat,它获取所有数据,但我只需要提供来源的某些数据

HTML

<tr ng-repeat="val in values ">
    <td ng-bind="$index"></td>
    <td ng-bind="val.rec">ED1500322</td> 
    <td>working</td>
    <td ng-bind="val.result">I am going to School</td>
    <td>
      <div class="radio">
        <input ng-model="val.iscorrect" value="yes" type="radio">
        <label for="opt1">yes</label>
        <input ng-model="val.iscorrect" value="no" type="radio">
        <label for="opt10">no</label>
      </div>
    </td>
  </tr>

数据

$scope.values = [{
    name: "John",
    rec:234,
    iscorrect: ''
  }, {
    name: "Paul",
    rec:44,
    iscorrect: ''
  }, {
    name: "George",
    rec:2664,
    iscorrect: 'no'
  }, {
    name: "Ringo",
    rec:124,
    iscorrect: 'yes'
  }];

我们可以看到我有一些iscorrect =“”的值。我只希望相应的值在我的$ scope.values和html

plunker http://plnkr.co/edit/f0l7MHN7UAvOLNZ8BCjV?p=preview

获取数据的任何帮助iscorrect =“”表示赞赏......

3 个答案:

答案 0 :(得分:3)

所以你只想展示那些iscorrect =&#39;&#39;?

试试这个:

{{1}}

修改了删除过滤器的答案,并使用ng-if而不是更清洁plnkr

答案 1 :(得分:2)

<color name="black">#99000000</color> 中使用以下内容过滤值

ng-repeat

然后在控制器中:

<tr ng-repeat="val in getValues(values) ">
  <td ng-bind="$index"></td>
  <td ng-bind="val.rec">ED1500322</td> 
  <td>working</td>
  <td ng-bind="val.result">I am going to School</td>
  <td>
   <div class="radio">
    <input ng-model="val.iscorrect" value="yes" type="radio">
    <label for="opt1">yes</label>
    <input ng-model="val.iscorrect" value="no" type="radio">
    <label for="opt10">no</label>
   </div>
  </td>
</tr>

我建议不要使用过滤器,因为它们会添加大量观察者并触发摘要周期,这对UI性能不利

更新:添加plunkr链接:http://plnkr.co/edit/3GJZca8YLnW4P0BGulfq?p=preview

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myController">
  <div ng-repeat="val in values | filter : val.iscorrect='no'">
    <input ng-model="val.iscorrect" value="yes" type="radio">
    <label for="opt1">yes</label>
    <input ng-model="val.iscorrect" value="no" type="radio">
    <label for="opt10">no</label>
  </div>
</div>
<script>
var app = angular.module("myApp", []);

app.controller("myController", function($scope) {
  $scope.values = [{
    name: "John",
    rec: 234,
    iscorrect: 'no'
  }, {
    name: "Paul",
    rec: 44,
    iscorrect: 'yes'
  }, {
    name: "George",
    rec: 2664,
    iscorrect: 'no'
  }, {
    name: "Ringo",
    rec: 124,
    iscorrect: 'yes'
  }];
});

</script>

</body>
</html>