簡體   English   中英

角度指令 ng-repeat 和范圍問題

[英]Angular directive ng-repeat and scope problemm

我有一些麻煩。 ng-repeat指令commentsFeedback中,我展示了一些用戶評論。 在單擊響應后的表單中,我從服務器獲取新對象。 我正在將$scope.users[0].atrb更新$scope.users[0].atrb response.result 但指令中的ng-repeat不會重寫此更新。

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更改后更新指令中的元素?

盡量不要將scope.users[0].atrb重新分配給scope.some_list並直接在ng-repeat使用它。 像這樣: <div ng-repeat='key in users[0].atrb'> 或者您可能只是可以將response.result分配給users[0].atrbsome_list (我只是不確定您在此處提供的代碼段背后的內容)? 否則我想你將不得不使用你在評論中描述的$watch

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM