2017-01-09 42 views
1

我有一些麻烦。在指令commentsFeedbackng-repeat我正在显示一些用户评论。 在表单响应后,我从服务器获取新对象。我正在更新$scope.users[0].atrb到response.result,但指令中的ng-repeat不会重写此更新。Angular指令ng-repeat和范围problemm

myApp.controller('myCtrl', ['$scope', '$http', 'Myfactory', 
    function ($scope, $http, Myfactory){ 
    $scope.commentFunk = function(comment, commentForm){ 
     $http.post('url/', {text: comment.text}) 
      .success(function (response) { 
       $scope.users[0].atrb = response.result; 
      }) 
      .error( function (response) { 
       console.log('Response', response); 
      }) 
    } 
}]); 
myApp.directive('commentsFeedback', function() { 
    return { 
     restrict: 'AE', 
     template: "<h2>Comments</h2>" + 
     "<div ng-repeat='key in some_list'>" + 
     "<div ng-repeat='item in key.comments_list'><h4>{$ key.name $}</h4> <b>{$ item.author $}:</b> {$ item.text $}" + 
     "</div></div>", 
     link:function (scope, element, attrs){ 
      scope.some_list = scope.users[0].atrb; 
     } 
     } 
    }); 

如何从服务器或如何更新元素指令scope.users[0].atrb后已改变越来越响应之后,重新显示在指令中的元素?

+0

嘿,这是我的决定。 (); 我插入到控制器'$ watch'并从指令delete'function()' '$ scope。$ watch( function(oldVal,newVal){ updateCommentsList(); }); 函数updateCommentsList(){ $ scope.somelist = $ scope.users [0] .atrb }' 是否有任何变种更有效地做到这一点? – VolArt

回答

1

尽量不要重新分配scope.users[0].atrbscope.some_list并直接在ng-repeat中使用它。像这样:<div ng-repeat='key in users[0].atrb'>。或者你可能只是可以将response.result分配给users[0].atrbsome_list(我只是不确定你在这里提供的代码背后有什么)?否则,我想你必须使用$watch,正如你在评论中所描述的那样。