Javascript 访问ngModel';指令链接内的父对象
在我的指令中要求使用ngModel并在我的link函数中使用第四个参数ngModel之后,我可以访问绑定模型的值。在我的实例中,这个绑定值是product.id(在ng repeat中) 我现在如何从链接函数读取产品对象的其他值Javascript 访问ngModel';指令链接内的父对象,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,在我的指令中要求使用ngModel并在我的link函数中使用第四个参数ngModel之后,我可以访问绑定模型的值。在我的实例中,这个绑定值是product.id(在ng repeat中) 我现在如何从链接函数读取产品对象的其他值 您可以使用输入数据标签绑定产品; 比如:- 我觉得您试图做一些错误的事情,甚至想在绑定到的指令中使用此功能。您想要实现什么?我必须为使用AJAX搜索产品的select2框设置默认值。它是id=product.id和text=product.name。那么为什么需要?默认
您可以使用输入数据标签绑定产品;
比如:-
我觉得您试图做一些错误的事情,甚至想在绑定到
的指令中使用此功能。您想要实现什么?我必须为使用AJAX搜索产品的select2框设置默认值。它是id=product.id和text=product.name。那么为什么需要
?默认值是通过设置输入的vm模型来设置的。如果要使用AJAX功能,则必须使用Select2的隐藏输入。由于必须在select2()函数中设置initialValue,因此这是唯一正确的方法。
app.directive('myDirective', function() {
restrict: "a",
require: "ngModel",
link: function(scope, element, attrs, ngModel) {
// Here I want to read product.name, product.price etc
}
});
<input type="hidden" ng-model="product.id" data-product="product" my-directive">
app.directive('myDirective', function() {
restrict: "a",
scope{
product: "=product"
},
link: function(scope, element, attrs, ngModel) {
// Here I want to read product.name, product.price etc
//Here scope.product will return all the values
}
});