[英]How to get ng-model value to directive
在以下指令中,我需要ng-model值。
app.directive('getRelatedDocumentDetails',['documentService',function(documentService){
return {
require: 'ngModel',
restrict: 'AE', //attribute or element
replace: true,
link: function(scope,element,attrs,ngModel){
element.on('change',function(element,attrs){
//Here I need ng-model
})
}
}
在上面的代码中,我没有在change函数中得到ng-model。
链接功能使您可以像在控制器中一样直接访问范围对象。
因此,您可以在此处直接访问用于ng-model
的范围对象
假设您如下定义html
<get-related-document-details ng-model="myVar">
</get-related-document-details>
然后控制器中的代码将像
app.directive('getRelatedDocumentDetails',['documentService',function(documentService){
return {
require: 'ngModel',
restrict: 'AE', //attribute or element
replace: true,
link: function(scope,element,attrs,ngModel){
element.on('change',function(element,attrs){
scope.myVar='something'; //here you can assign value to ng-model scope variable
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.