【发布时间】:2016-12-01 01:38:10
【问题描述】:
我当前的实现如下所示,它正在工作,但由于 getImage 方法在表达式内部,当任何文本更改或在页面上输入数据时,它会被重复调用
HTML
<div ng-repeat="product in pageData.products">
<div>
<img style="width:60px;" ng-src={{getImage(product)}} />
</div>
</div>
JS:
$scope.getImage = function(product)
{
var image = Utils.getLocalStorageEntityByIdFromList(product.ID,"ProductID", "images");
if(image) return image.Data;
else return "";
};
我正在尝试将 getImage 方法放在 .directive 中,因为大多数帖子都建议并将产品作为参数传递到指令中。 发布链接 How to call a function in "ng-src"
我是 AngularJS 的新手,如果有任何帮助将 getImage 方法放入可以传递产品参数的指令中,我将非常感激。
如下所示。
JS:
angular.module('MyApp', [])
.controller('Ctrl2', function($scope) {
})
.directive('mySrc', function() {
return {
restrict: 'A',
link: function ( scope, elem, attrs ) {
var image = Utils.getLocalStorageEntityByIdFromList(product.ID,"ProductID", "images");
elem.attr('src', image.Data);
}
};
});
HTML:
<div ng-app="MyApp">
<div ng-controller="Ctrl2">
<div ng-repeat="product in pageData.products">
<div><img my-src="product" /><div>
</div>
</div>
非常感谢您的宝贵时间。
【问题讨论】:
-
使用此方法更新您的帖子并 getLocalStorageEntityByIdFromList 通知我
-
您好 Aravid,感谢您的调查。对不起,我不明白你的意思,带有 .directive 的第二段 JS 代码目前无法正常工作,这就是我不确定的......
标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat