[英]Angular.js: Data binding not working with directives controller option
當我在指令controller選項中指定控制器時,為什么angular的數據綁定不起作用? $scope.emailInvalid.text
通常應該映射到type.text
但是在我的情況下,什么也沒有顯示。
JS:
.directive('alert', function () {
return {
template: '<div>{{type.text}}</div>',
restrict: 'E',
scope: {
type: '='
},
controller: function ($scope) {
$scope.emailInvalid = {
text: 'Text Alert Two'
};
}
};
});
HTML:
<alert type="emailInvalid"></alert>
當我定義一個單獨的控制器並將其與ng-controller
一起傳遞到HTML中時,一切都會按預期進行。
這是一個矮人 。
好的,我找到了解決方案:
問題是, $scope.emailInvalid
無法將$scope.emailInvalid
映射到$scope.type
。 在我的示例中,使用<alert type="emailInvalid"></alert>
所做的事情是將對象emailInvalid
傳遞給指令。 Angular在我使用指令的范圍模型中尋找此對象。 顯然,該對象不存在,角度無法映射任何內容。
我缺少的部分是, 我用指令controller選項定義的控制器的定義范圍不同於與ng-controller
一起使用ng-controller
。
要變通解決此問題,我現在傳遞一個字符串而不是一個對象,並使用switch / case映射警報類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.