且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AngularJS 指令中的可选表达式属性

更新时间:2023-02-21 19:56:44

你可以通过将你的 $eval 调用更改为

You could make what you have work by chaning your $eval call to

scope.$parent.$eval(attrs.disable);

因为您需要计算父作用域中 attrs.disable 中包含的表达式,而不是指令的隔离作用域.但是,由于您使用的是&"语法,它将自动评估父作用域中的表达式.所以只需执行以下操作:

because you need to evaluate the expression contained in attrs.disable in the parent scope, not the directive's isolate scope. However, since you are using the '&' syntax, it will evaluate the expression in the parent scope automatically. So just do the following instead:

if(angular.isDefined(attrs.disable)) {
    scope.disable();
}

小提琴.