在指令中访问 ngModel 的父对象 link

Access ngModel's parent object inside directive link

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

我现在如何从我的 link 函数中读取我的产品对象的其他值?

<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

   }
});