angularjs – Angular – 无法在ng-switch-when内观看

前端之家收集整理的这篇文章主要介绍了angularjs – Angular – 无法在ng-switch-when内观看前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要$观察范围的变量,但它永远不会被调用.
我发现它与ng-switch-when有关.

例如:
http://jsfiddle.net/gGKGX/

angular.module("GuyModule",[]).controller("GuyController",function($scope) {

    $scope.something = "foo";

      $scope.guy="This is guy";

    $scope.$watch('guy',function(){ console.log("guy changed!"); }  );

} );


<div  ng-controller="GuyController" ng-app="GuyModule">
    <div ng-switch="something">
        <div ng-switch-when="foo">
            <input ng-model="guy"/>
        </div>
        <div ng-switch-when="bar">
            <button ng-click="something='bar'">Click me!</button>
        </div>
    </div>
</div>

我希望“家伙改变了!”要打印但不是,当我删除“ng-switch-when”时它可以工作.
我该如何解决这个问题?

顺便说一句 – 同样的事情发生在ui-if(来自angularui项目).

解决方法

家伙是在开关和控制器的范围内.

正如您在图片中看到的那样,两个范围都存在.在编辑它时,您正在ng-switch的范围内进行编辑,而不是正在编辑您正在观看的那个

另一个例子是在交换机外显示人:http://jsfiddle.net/TheSharpieOne/gGKGX/1/

解决此问题:将$parent添加到交换机中的人($parent.guy):http://jsfiddle.net/TheSharpieOne/gGKGX/2/

猜你在找的Angularjs相关文章