【发布时间】:2016-03-31 15:27:47
【问题描述】:
我正在尝试创建一个指令,该指令将禁用容器内的所有输入元素,但我遇到了属性问题。这就是我得到的指令
指令
angular.module('common')
.directive('bdDisabled', [function () {
return {
restrict: 'A',
scope: {
bdDisabled: '='
},
link: function (scope, element, attrs) {
attrs.$observe('bdDisabled', function(newValue, oldValue) {
if (newValue !== oldValue) {
if (newValue) {
element.find('input, select, button, submit, textarea').prop('disabled', 'disabled');
} else {
element.find('input, select, button, submit, textarea').removeProp('disabled');
}
}
});
}
};
}
]);
这就是我想要的使用方式:
<div class="cg-content cg-shadow" bd-disabled="isLoading">
问题是属性值是字符串isLoading而不是值。
如果我将它用大括号括起来,它会中断,并且我会在控制台中收到错误消息。 如果我将它包裹在大括号中并将范围更改为“@”而不是“=”,它就可以工作。但它发送字符串“true”或“false”,而不是布尔值。
我哪里出错了?
【问题讨论】:
-
如果将
attrs.$observe切换为scope.$watch会发生什么? -
我试试看,等一下
-
使用 @ 是为了传递一种方式绑定字符串。您可以将其解析为布尔值,否则将其传递为 =,这将是双向绑定对象
-
@OmriAharon,做到了。谢谢。请建议它作为答案,我会接受。
标签: javascript angularjs angular-directive