在指令链接中访问 ngModel 的父对象

Access ngModel's parent object inside directive link

本文关键字:对象 ngModel 访问 指令 链接      更新时间:2023-09-26

在我的指令中要求ngModel并在链接函数中使用第4个参数ngModel之后,我可以访问绑定模型的值。在我的实例中,这个绑定值是 product.id 的(在 ng 重复内)。

现在如何从链接函数读取产品对象的其他值?

<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
   }
});