看着角度的表情

时间:2015-08-25 19:20:23

标签: angularjs angularjs-scope ng-repeat watch

是否可以观看ng-repeat表达?我正在处理实时数据,我想观察更改的表达式值。做这个的最好方式是什么。也许更好的问题是如何在一组对象上观察键。这是我的代码

//MY CONTROLLER//
        $http.get('myJsonUrl')
                .then(function (person) {
                    $scope.personInfo = person.data;
                    $scope.val = person.data.val <--not working

                    console.log('returnedData', $scope.personInfo, $scope.val);
                });

HTML
 <table>
    <tr ng-repeat ='data in personInfo" >
    <td>{{data.val}}</td>  <--I want to watch this in scope
    </tr>
 </table>

1 个答案:

答案 0 :(得分:0)

You can instantiate a new controller (and watch) for every row in the ng-repeat.

<table>
  <tr ng-repeat ="data in personInfo" ng-controller="PersonInfoController">
  <td>{{data.val}}</td>  <--I want to watch this in scope
</tr>

angular.module(...).controller('PersonInfoController', function ($scope) {
    $scope.$watch('data.val', function (n, o) {
        ...
        // from here you can also access your parent controller e.g.
        $scope.someParentMethod(n);
    });
});

based on the answer found here Setting $scope.$watch on each element in an array