2015-10-23 53 views
0

我試圖在列表中顯示圖像。圖像的路徑存儲在名爲imagesUrls的數組中。如何從AngularJS模板訪問數組

imagesUrls['dhd.png'] = "./img/brands/dhd.png" imagesUrls['channelislands.png'] = "./img/brands/channelislands.png"

等等

<ion-item collection-repeat="item in prodataSelect | unique:'brand' | orderBy:'brand'" collection-item-height="75" > 
    <img class="selectBrandImage" ng-src="imagesUrls['{{item.brand | nospace | lowercase}}.png']" /> 
    <span classs="selectBrandName">{{item.brand}}</span> 
</ion-item> 

controllers.js:

prodata = sessionService.get('prodata'); 
$scope.prodataSelect = prodata; 
$scope.imagesUrls = sessionService.get('imagesUrls'); 

我找不到模板的ng-src正確的語法,你能幫助請?

我也試過這樣:

ng-src="getImgPath(item.brand)" 

與此:

$scope.getImgPath = function(str) { 
    return imagesUrls[$filter('nospace')($filter('lowercase')(str))]; 
} 
+0

當您檢查瀏覽器中的元素時,src的值是多少?你有我們可以玩的小提琴嗎? – ltalhouarne

+0

當我檢查的值是src =「imagesUrls ['carroll.png']」 – Louis

+0

你試過src而不是ng-src嗎? – ltalhouarne

回答

1

它的工作原理與此:

ng-src="{{getImgPath(item.brand)}}" 

$scope.getImgPath = function(str) { 
    str = $filter('nospace')($filter('lowercase')(str)); 
    str = str + ".png"; 
    return $scope.imagesUrls[str]; 
    }