簡體   English   中英

AngularJs-打破ng控制器

[英]AngularJs - transclude break the ng-controller

我對角度有深刻的了解,我無法理解我做錯了什么。

我有一個由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
只需單擊“單擊我”按鈕即可查看結果。

輸入值更改后, showPass()ng-model引用不同的作用域。 請參閱此SO答案以獲得更深入的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM