简体   繁体   English

角度指令 ng-repeat 和范围问题

[英]Angular directive ng-repeat and scope problemm

I have some troubles.我有一些麻烦。 In directive commentsFeedback in ng-repeat I'm showing some user comments.ng-repeat指令commentsFeedback中,我展示了一些用户评论。 In form after click in response i get new object from server .在单击响应后的表单中,我从服务器获取新对象。 I'm updating $scope.users[0].atrb to response.result but ng-repeat in directive don't rewrite this updates.我正在将$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;
        }
      }
   });

How to re shown elements in directive after getting response from server or how to update elements in directive after scope.users[0].atrb have been changed?如何在从服务器获得响应后重新显示指令中的元素或如何在scope.users[0].atrb更改后更新指令中的元素?

Try just to don't reassign scope.users[0].atrb to scope.some_list and use it directly in ng-repeat .尽量不要将scope.users[0].atrb重新分配给scope.some_list并直接在ng-repeat使用它。 Like so: <div ng-repeat='key in users[0].atrb'> .像这样: <div ng-repeat='key in users[0].atrb'> Or may be you just can assign response.result to both users[0].atrb and some_list (I'm just not sure what behind the piece of code you presented here)?或者您可能只是可以将response.result分配给users[0].atrbsome_list (我只是不确定您在此处提供的代码段背后的内容)? Otherwise I suppose you'll have to use $watch as you described in your comment.否则我想你将不得不使用你在评论中描述的$watch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM