我需要能够调用一个函数来运行代码来动态检索图像的来源.以下代码段显示了我想要的示例:
<!-- "myFunction" exists in the current scope -->
<img ng-src="myFunction()" />
Run Code Online (Sandbox Code Playgroud)
我确定这必须简单但我在ng-src文档中找不到任何东西!还有人打过这个吗?
提前致谢!
指令(基于答案的示例)
其他人建议指示.我无法发布客户端代码,所以我写了一个简短的例子,说明在plunker中会是什么样子(见这里).核心指令本身是:
app.directive("imageSource", function (){
return { link: function (scope, element, attrs){
element.attr("src", scope.imageUrlArray[attrs.imageSource]);
}
};
});
Run Code Online (Sandbox Code Playgroud)
我知道我在这里作为一个例子可能只是使用ng-src中的变量来完成ng-repeat,但它可以作为一个指令在需要时的样子的例子.
Alw*_*ner 87
<img ng-src="{{myFunction()}}" />
Run Code Online (Sandbox Code Playgroud)
对,最后到达那里:
angular.module('MyApp', [])
.controller('Ctrl2', function($scope) {
})
.directive('mySrc', function() {
return {
restrict: 'A',
link: function ( scope, elem, attrs ) {
//replace test with whatever myFunction() does
elem.attr('src', 'test1');
}
};
});
Run Code Online (Sandbox Code Playgroud)
<div ng-app="MyApp">
<div ng-controller="Ctrl2">
<img my-src />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38842 次 |
| 最近记录: |