javascript - 在指令链接内访问 ngModel 的父对象

标签 javascript angularjs angularjs-directive angularjs-scope

在我的指令中要求 ngModel 并在链接函数中使用第四个参数 ngModel 后,我可以访问绑定(bind)模型的值。在我的实例中,这个绑定(bind)值是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
   }
});

最佳答案

可以将产品与输入数据标签绑定(bind); 喜欢:-

<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

   }
});

关于javascript - 在指令链接内访问 ngModel 的父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28319610/

相关文章:

angularjs - 使用 Angular JS 和基于 Spring 的 RESTful Web 服务下载文件

javascript - HTML 中的 Angular 访问指令 Controller 变量

javascript - 将函数传递给 Angular 验证指令

javascript - 如何将 Javascript 用户的用户数据返回到 Django

javascript - 如何为此日期选择器示例创建指令?

javascript - 编写一个函数来平滑指定范围内的中心值

javascript - 仅在 li ng-click 上切换 1 个子菜单,不允许打开另一个 li

javascript - 限制 AngularJS 中的 $scope

javascript - 显示被单击的特定项目列表的子内容。

javascript - 如何在 Cordova 中从一个 Js 访问一个 Sql lite 变量到另一个 Js