简体   繁体   English

指令听取控制器变量的变化

[英]Directive listen to controller variable change

Just starting out in AngularJS and trying to figure out the best practice for listening to events when a variable within a controller changes. 刚开始使用AngularJS并尝试找出控制器中的变量发生变化时监听事件的最佳实践。 The only way I have gotten it to work is with an emit, as follows. 我得到它的唯一方法是使用发射,如下所示。

For example: 例如:

var app = angular.module("sampleApp", [])

app.controller("AppCtrl", function($scope){
  $scope.elements = [
    {
        name: "test"
    }
  ]

  $scope.addElement = function() {
      $scope.elements.push({
          name: "test" + $scope.elements.length
      })
      $scope.$emit('elementsChanged', $scope.elements);
  }
})

app.directive('render', function() {
  var renderFunc = function() {
      console.log("model updated");
  }
  return {
      restrict: 'E',
      link: function(scope, element, attrs, ngModel) {
         scope.$on('elementsChanged', function(event, args) {
              renderFunc();
         })
      }
  }
})

This seems a bit wonky, and I feel like I'm working against the point of angular. 这看起来有点不稳定,我觉得我正在反对角度。 I've tried to have a $watch on a model, but that doesn't seem to be working. 我试图在模型上有一个$ watch,但这似乎不起作用。 Any help on this would be very appreciated, thanks! 对此有任何帮助将非常感谢,谢谢!

I'm going to assume you're using unstable Angular, because $watchCollection is only in the unstable branch. 我假设你使用的是不稳定的Angular,因为$ watchCollection只在不稳定的分支中。

$watchCollection(obj, listener)

Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). Shallow监视对象的属性,并在任何属性发生更改时触发(对于数组,这意味着要观察数组项;对于对象映射,这意味着要观察属性)。 If a change is detected, the listener callback is fired. 如果检测到更改,则会触发侦听器回调。

The 'Angular' way of doing this would be to watch an attribute in your directive. 'Angular'这样做的方法是在你的指令中观察一个属性。

<render collection='elements'></render>

Your directive 你的指示

app.directive('render', function() {
  var renderFunc = function() {
      console.log("model updated");
  }
  return {
      restrict: 'E',
      link: function(scope, element, attrs) {
         scope.$watchCollection(attrs.collection, function(val) {
           renderFunc();
         });
      }
  }
})

If you're doing this on stable angular, you can pass true as the last argument to scope.$watch, which will watch for equality rather than reference. 如果你在稳定角度上执行此操作,则可以将true作为最后一个参数传递给范围。$ watch,它将监视相等而不是引用。

$watch(watchExpression, listener, objectEquality)

objectEquality (optional) boolean Compare object for equality rather than for reference. objectEquality(可选)boolean比较对象是否相等而不是引用。

What's happening here is the collection attribute on the DOM element specifies which property on our scope we should watch. 这里发生的是DOM元素上的collection属性指定我们应该观察的范围上的哪个属性。 The $watchCollection function callback will be executed anytime that value changes on the scope, so we can safely fire the renderFunc(). $ watchCollection函数回调将在范围内的值发生变化时执行,因此我们可以安全地触发renderFunc()。

Events in Angular should really not be used that often. Angular中的事件通常不应该被使用。 You were right in thinking there was a better way. 你认为有更好的方法是正确的。 Hopefully this helps. 希望这会有所帮助。

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

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