Angularjs 如何在Angular JS中避免$watch服务?

Angularjs 如何在Angular JS中避免$watch服务?,angularjs,Angularjs,我只是想避免在我的节目中使用$watch服务。有什么替代方法吗?您可以通过以下策略避免使用watch: $scope通信 ngModel$parsers ngModel$viewChangeListeners 设定间隔 过滤器 使用setInterval的示例: <div ng-app="MyApp"> <div ng-controller="MyCtrl"> <input ng-model="myModel.myProp"> <br

我只是想避免在我的节目中使用$watch服务。有什么替代方法吗?

您可以通过以下策略避免使用watch:

  • $scope通信
  • ngModel$parsers
  • ngModel$viewChangeListeners
  • 设定间隔
  • 过滤器
  • 使用setInterval的示例:

    <div ng-app="MyApp">
      <div ng-controller="MyCtrl">
        <input ng-model="myModel.myProp">
        <br>
        <input ng-model="myModel.myProp2">
      </div>
    </div>
    
    <script>
      "use strict";
      angular.notifyMe = function(scope, expr, callbackFn) {
        var oldValue = scope.$eval(expr);
        setInterval(function() {
          var newValue = scope.$eval(expr);
          if (newValue !== oldValue) {
            setTimeout(function() {
              callbackFn.call(null, newValue, oldValue);
              oldValue = newValue;
            },0);
          }
        }, 100);
      };
      angular.module("MyApp", [])
      .controller("MyCtrl", function($scope, $filter) {
        $scope.myModel = {
          myProp: "Test Value",
          myProp2: "Test Value 2"
        };
        angular.notifyMe($scope, "myModel.myProp", function(newValue, oldValue) {
          console.log("int myProp value changed, new:" + newValue + ", old: " + oldValue);
        });
        angular.notifyMe($scope, "myModel.myProp2", function(newValue, oldValue) {
          console.log("int myProp2 value changed, new:" + newValue + ", old: " + oldValue);
        });
      });
    </script>
    
    您仍然可以在angular 1.5上使用

    这个钩子允许我们对组件的单向绑定的更改做出反应。Angular 1.5中还引入了单向绑定,并将其与Angular 2的单向数据流更加协调。假设我们使用单向绑定从外部世界配置myCmp的name属性:

    mod.component('myCmp', {
      template: '<h1>{{$ctrl.name}}</h1>',
      bindings: {
        name: '<'
      },
      controller: MyCmpController
    });
    

    您可以通过以下策略避免使用watch:

  • $scope通信
  • ngModel$parsers
  • ngModel$viewChangeListeners
  • 设定间隔
  • 过滤器
  • 使用setInterval的示例:

    <div ng-app="MyApp">
      <div ng-controller="MyCtrl">
        <input ng-model="myModel.myProp">
        <br>
        <input ng-model="myModel.myProp2">
      </div>
    </div>
    
    <script>
      "use strict";
      angular.notifyMe = function(scope, expr, callbackFn) {
        var oldValue = scope.$eval(expr);
        setInterval(function() {
          var newValue = scope.$eval(expr);
          if (newValue !== oldValue) {
            setTimeout(function() {
              callbackFn.call(null, newValue, oldValue);
              oldValue = newValue;
            },0);
          }
        }, 100);
      };
      angular.module("MyApp", [])
      .controller("MyCtrl", function($scope, $filter) {
        $scope.myModel = {
          myProp: "Test Value",
          myProp2: "Test Value 2"
        };
        angular.notifyMe($scope, "myModel.myProp", function(newValue, oldValue) {
          console.log("int myProp value changed, new:" + newValue + ", old: " + oldValue);
        });
        angular.notifyMe($scope, "myModel.myProp2", function(newValue, oldValue) {
          console.log("int myProp2 value changed, new:" + newValue + ", old: " + oldValue);
        });
      });
    </script>
    
    您仍然可以在angular 1.5上使用

    这个钩子允许我们对组件的单向绑定的更改做出反应。Angular 1.5中还引入了单向绑定,并将其与Angular 2的单向数据流更加协调。假设我们使用单向绑定从外部世界配置myCmp的name属性:

    mod.component('myCmp', {
      template: '<h1>{{$ctrl.name}}</h1>',
      bindings: {
        name: '<'
      },
      controller: MyCmpController
    });
    

    我为自己找到的最简单的策略是完全避免
    $scope
    。使用
    控制器as
    将控制器绑定到作用域,并直接与控制器交互;然后,您可以使用简单的Javascript机制来检测更改。例如,ES5 getter/setter:

    class FooController {
        get bar() {
            return this._bar;
        }
    
        set bar(value) {
            this._bar = value;
            console.log(this._bar);
        }
    }
    
    angular.module('Baz', []).controller('FooController', FooController);
    
    
    ...
    
    我为自己找到的最简单的策略是完全避免
    $scope
    。使用
    控制器as
    将控制器绑定到作用域,并直接与控制器交互;然后,您可以使用简单的Javascript机制来检测更改。例如,ES5 getter/setter:

    class FooController {
        get bar() {
            return this._bar;
        }
    
        set bar(value) {
            this._bar = value;
            console.log(this._bar);
        }
    }
    
    angular.module('Baz', []).controller('FooController', FooController);
    
    
    ...
    
    请发布您的代码并说明您想要的位置您可以提供一个手表的示例吗?那么也许我们可以帮助你为什么要避免$watch?对不起,你被不认识angularJS的人否决了。请发布你的代码并说明你想要什么,你可以提供一个你使用的手表的示例吗?那么也许我们可以帮你为什么要避免$watch?对不起,你被不认识angularJS的人否决了。你可以添加组件$onChanges(),你可以添加组件$onChanges()
    <div ng-controller="FooController as ctrl">
        <input ng-model="ctrl.bar">
        ...