我在刷新/更新角度指令时遇到问题。基本上有一个标志按钮,将异步调用服务器,如果成功,更改属性userFlags应禁用该按钮,并将其文本更改为“标记...”。角度指令不更新
这里是指令:
app.directive('flagable', function() {
return {
restrict: 'E',
scope: { item: '=' },
templateUrl: '/Content/templates/flagable.tmpl',
controller: function($scope) {
$scope.flag = function() {
$scope.$apply(function() {
// ITS NOT GOING IN HERE!
//AJAX CALL HERE
model.$modelValue.userFlags = [];
Messenger().post("Your request has succeded! ");
});
};
}
};
});
这里是模板:
<div class="btn-group pull-right">
<button class="btn btn-small btn-danger" ng-disabled="{{item.userFlags != null}}"><i class="icon-flag"></i>
<any ng-switch="item.userFlags==null">
<any ng-switch-when="true">Flag</any>
<any ng-switch-when="false">Flagged...</any>
</any>
</button>
<button class="btn btn-small btn-danger dropdown-toggle" data-toggle="dropdown" ng-disabled="{{item.userFlags != null}}"><span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" ng-click="flag()">Inappropriate</a></li>
<li><a href="#" ng-click="flag()">Overpost</a></li>
<li><a href="#" ng-click="flag()">Spam</a></li>
</ul>
</div>
有趣的是,改变控制逻辑:
$scope.flag = function() {
$scope.item.userFlags = [];
Messenger().post("Your request has succeded! " + $scope.item.viewCount);
};
原因为按钮正确刷新到“已标记...”,但是,ng-d它不会使按钮失效!在Firebug它表明了NG-disabled属性设置: NG-禁用= “真正的”
你看到错误? 'ngClick'开始一个'$ apply'调用,所以再次从click处理程序中执行这个操作是错误的。 –
只需将其更改为$ scope.item.userFlags = [];邮件(“您的请求已成功!”+ $ scope.item.viewCount);并删除范围应用换行不会刷新该指令,但会出现警报。 – Val