在我的指令中要求 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/