ng-switch创造了多少手表?

时间:2015-04-25 20:11:50

标签: angularjs

简单: ng-switch有多少手表?

例如:

    <section ng-switch="value">
        <div ng-switch-when="1">1</div>
        <div ng-switch-when="2">2</div>
        <div ng-switch-when="3">3</div>
        <div ng-switch-when="4">4</div>
    </section>

这会创造一只手表吗? 4?

提前致谢。

1 个答案:

答案 0 :(得分:4)

ngSwitchng-switch属性中的表达式上创建一个观察者。

一种简单的检查方法是在控制器中执行以下操作:

// $timeout is a crude way to "wait" until ng-switch sets up the watcher
$timeout(function(){
  console.log($scope.$$watchers ? $scope.$$watchers.length : 0);
})

另一个,就是这样做(减1是因为这个表达引入了自己的$ watch):

<pre>{{$$watchers.length - 1}}</pre>
<section ng-switch="value">
  <div ng-switch-when="1">...</div>
  ...
</section>

另一个是,通过转到source并在那里看到一个$scope.$watch :) - 当然,还有没有隔离范围绑定的事实