简体   繁体   English

如何在不使用$ watch的情况下更新作用域值

[英]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.

相关问题 如何在不使用服务范围angularjs的情况下观察服务价值的变化 - how to $watch a change on service value without using the services scope angularjs 如何定义变量而不使用$ scope $ watch in angular? - How to wait until a variable is defined without using $scope $watch in angular? 使用没有$ scope的$ watch(控制器作为语法) - Using $watch without $scope ( controller as syntax) 如何持续监视范围变量直到更新 - how to continuously watch for scope variable until update 如何使用AngularJS范围观察JQuery选择器。$ watch()方法 - How To Watch a JQuery Selector Using AngularJS scope.$watch() Method 监视不在$ scope上的变量以更新位于$ scope上的变量 - Watch variable not on $scope to update a variable which is on $scope 如何在Angular 1.5组件中等待绑定(没有$ scope。$ watch) - How to wait for binding in Angular 1.5 component (without $scope.$watch) Angular $ watch如何确定$ scope.value的变化方式? - How an Angular $watch to indentity how the $scope.value changed? 范围值更改时如何使用angularJs更新UI? - How to update UI using angularJs when scope value changes? Angular.js:在ngRepeat中传递属性时,如何在不使用隔离范围的情况下在指令中更新父范围 - Angularjs: How to update parent scope in directive without using isolated scope when the attribute is passed in within ngRepeat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM