我对 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/