AngularJs - transclude打破ng-controller

时间:2014-11-20 11:12:24

标签: javascript angularjs angularjs-directive controller angularjs-ng-transclude

我对棱角有深刻的理解,我无法理解我做错了什么。

我有一个由ng-controller包围的指令。

 KApp.directive("testdirective", function ($compile)
    {
        return {
            restrict: 'E',
            transclude: true,
            scope: {
                test:"="
            },
            template: "<div>\
                           <div style='width:120px'>\
                           {{test}}\
                            </div>\
                            <div  ng-transclude>\
                          </div>\
                       </div>"
              };
    });


  KApp.controller('testCtrl', function ($scope)
    {

         $scope.someText ="not important"

         $scope.pass = "1234";

         $scope.showPass = function()
          {
            //why the $scope.pass not updated via ng-model???
           alert($scope.pass)
         }


    });

HTML

<body ng-app="mainModule" ng-controller="appCtrl">
<div ng-controller="tsetCtrl">
 <testdirective  test="someText">
    <button style='width:120px' ng-click='showPass()'>
     Click me
            </button>
         <input ng-model='pass' style='width:120px'>
 </testdirective>
</div>

ng-model="pass"绑定到$scope.pass = "1234";,用户应该更新。

我的问题:

视图$scope.pass 未更新,为什么?

以下是完整演示 - http://plnkr.co/edit/MsKm0LZtlQ45Yyq5Uw0d?p=preview
只需点击“点击我”按钮即可查看结果。

1 个答案:

答案 0 :(得分:1)

showPass()ng-model在输入值发生变化后引用不同的范围。有关更深入的解释,请参阅this SO answer

相关问题