2017-01-09 41 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,正如你在評論中所描述的那樣。