2017-01-09 20 views
1

私はいくつかの問題があります。指示commentsFeedbackng-repeatに私はユーザーのコメントをいくつか示しています。 フォームをクリックした後、私はサーバーから新しいオブジェクトを取得します。私は$scope.users[0].atrbをresponse.resultに更新していますが、指示のng-repeatはこのアップデートを書き換えません。 scope.users[0].atrbが変更された後、サーバーまたはどのディレクティブの要素を更新するための応答を取得した後ディレクティブ内の要素を示した再する方法角度指令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; 
     } 
     } 
    }); 

+0

Heh、それは私の決定です。 コントローラー '$ watch'に挿入し、' link:function() ' ' $ scope. $ watch( function(oldVal、newVal){ updateCommentsList(); }}ディレクティブから削除しました。 function 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.resultusers[0].atrbsome_listの両方に割り当てることができますか(ここで紹介したコードの後ろには何がわかりませんか)。それ以外の場合は、あなたのコメントに記載されているように$watchを使用しなければならないと思います。

関連する問題