在我的指令中要求ngModel并在链接函数中使用第四个参数ngModel之后,我可以访问绑定模型的值。在我的实例中,此绑定值是product.id(在ng-repeat内部)。
现在如何从链接函数读取产品对象的其他值?
<input type="hidden" ng-model="product.id" my-directive">
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
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句