javascript - AngularJs - 嵌入打破 ng-controller

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

我对 Angular 有很深的了解,但我无法理解我做错了什么。

我有一个被 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" 绑定(bind)到 $scope.pass = "1234"; 并且应该由用户更新。

我的问题:

$scope.pass 没有被 View 更新,为什么?

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

最佳答案

showPass()ng-model 在输入值更改后引用不同的范围。请参阅this SO answer以获得更深入的解释。

关于javascript - AngularJs - 嵌入打破 ng-controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27038150/

相关文章:

javascript - 响应式 Canvas 元素

javascript - 正文中的元标记

javascript - 使用 AngularJS 更新 CSS

javascript - ng-click 的奇怪错误

AngularJS - 从嵌入范围访问指令范围

javascript - 如何让 browserify 的 "bundle"函数发出结束事件?

javascript - 数据表标题在页面加载时不是全宽

javascript - AngularJS:将函数传递给指令

javascript - AngularJS - 将 $setValidity 与 ng-repeat 指令一起使用

angularjs - 与指令的两种方式数据绑定(bind)不起作用