Angularjs 是否可以要求角';的属性存在;谁的指令?
我创建了一个指令,它需要特定的属性来表示。Angularjs 是否可以要求角';的属性存在;谁的指令?,angularjs,Angularjs,我创建了一个指令,它需要特定的属性来表示。 我可以在link函数中检查属性的值并抛出异常。 不过,如果存在某种声明性方式,并且$compile像在需要特定控制器时一样抛出异常,那就更好了。您可以始终在指令中检查属性是否存在: link: function(scope, element, attrs) { if (!scope.title) { return false;//title attribute shouldn't be empty
我可以在link函数中检查属性的值并抛出异常。
不过,如果存在某种声明性方式,并且
$compile
像在需要特定控制器时一样抛出异常,那就更好了。您可以始终在指令中检查属性是否存在:
link: function(scope, element, attrs) {
if (!scope.title) {
return false;//title attribute shouldn't be empty
}
},
在指令定义的
compile
函数中检查并验证属性it。例如,这就是Angular的本机指令的构建方式
compile: function ngRepeatCompile($element, $attr) {
var expression = $attr.ngRepeat;
var match = expression.match(...);
if (!match) {
throw ngRepeatMinErr(...);
}
}