[英]How to update the scope value without using $watch
I have using custom directive 我有使用自定义指令
<users stats="stats"></users>
When we change the scope object from main controller, i have updating directive scope value also 当我们从主控制器更改范围对象时,我也有更新指令范围值
app.directive('users', function(){
var directive = {};
directive.restrict = "E";
directive.templateUrl = "templates/partials/users_stats.html";
directive.scope = {stats:'='};
directive.controller = "userStatsCtrl"
return directive;
});
So, Inside the directive controller, i am doing some func. 所以,在指令控制器内部,我正在做一些功能。 like
喜欢
app.controller("userStatsCtrl", function($scope){
$scope.$watch('stats', function(newValue) {
if (angular.isDefined(newValue)) {
.....
}
});
})
So here, i am using $watch to listen the scope, If it is update, I will do the some perform. 所以在这里,我使用$ watch来监听范围,如果是更新,我会做一些表演。
my question is, I dont want to use watch to listen the scope, If scope gets updated, i need to do some perform. 我的问题是,我不想用watch监听范围,如果范围得到更新,我需要做一些表演。
So how to update scope value without using scope.$watch 那么如何在不使用范围的情况下更新范围值。$ watch
When ever "scope.stats" value get changed in your main controller, you can broadcast an event and receive the same event in your directive and do operation what ever you want. 当主控制器中的“scope.stats”值发生变化时,您可以在指令中广播事件并接收相同的事件,并按照您的需要进行操作。 Example code for broadcast an event:
广播事件的示例代码:
$scope.$broadcast('yourEventName');
Receive an event in directive: 收到指令中的事件:
$scope.$on('yourEventName', function(){
});
how about trying: 尝试如何:
<users stats="stats" ng-change="onUserStatChange($event)"></users>
or
<users stats="stats" ng-blur="onUserStatChange($event)"></users>
and then in controller: 然后在控制器中:
$scope.onUserStatChange = function(event) {
//event.whatever
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.