Javascript 如何设置指令属性的默认值?
我正在用AngularJS构建一个自定义元素。我的元素将按如下方式使用:Javascript 如何设置指令属性的默认值?,javascript,angularjs,Javascript,Angularjs,我正在用AngularJS构建一个自定义元素。我的元素将按如下方式使用: <my-element my-attribute="false"></my-element> 谢谢大家! 将属性设置为可选 首先,确保属性是可选的。对于您当前的代码,如果您的指令是这样使用的 <!-- Notice the absence of attribute --> <my-element></my-element> 定义默认值 然后,您只需在控制器中
<my-element my-attribute="false"></my-element>
谢谢大家! 将属性设置为可选
首先,确保属性是可选的。对于您当前的代码,如果您的指令是这样使用的
<!-- Notice the absence of attribute -->
<my-element></my-element>
定义默认值
然后,您只需在控制器中设置默认值:
controller: function ($scope) {
if (angular.isUndefined($scope.myAttribute)) {
$scope.myAttribute = true;
}
},
scope: {
myAttribute: '=?',
},
controller: function ($scope) {
if (angular.isUndefined($scope.myAttribute)) {
$scope.myAttribute = true;
}
},