[英]custom validation directive in angular
我想为我的角度应用程序创建自定义验证指令。 唯一的问题是我不知道如何获得价值
<select class="form-control" ng-model="$parent.newSector" ng-options="item.id as item.name for item in sectors" not-empty-array="merchant.sectors"></select>
正如您从我的标记中看到的那样,我有一个名为notEmptyArray的指令,其中设置了一个表达式(它与ng-model相同,只是一个不同的表达式)。 现在如何在指令中访问它?
directive = function({
require: "ngModel",
link: function(scope, element, attributes, control) {
//how to access the value of merchant.sectors (its an array in the scope)
}
});
您需要隔离范围:
app.directive("notEmptyArray",function(){
return {
scope:{
items:"=notEmptyArray"
},
link: function(scope, element, attributes, control) {
/* anything you feed to "not-empty-array" would be accessible
inside items */
console.log(scope.items);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.