如何从AngularJS模板访问数组

时间:2015-10-23 14:10:05

标签: angularjs ionic

我正在尝试在列表中显示图像。图像的路径存储在名为imagesUrls的数组中。

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))];
}

1 个答案:

答案 0 :(得分:1)

适用于此:

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

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