动态NG-初始化变量 - Angularjs

动态NG-初始化变量 -  Angularjs

问题描述:

.coffee

@FooCtrl = ->
  $scope.products = Product.query()

的.html

<div ng-repeat='product in products'>
  <div ng-init='images_{{product.id}} = product.images' >
    <div class='slideshow' ng-repeat='pic in images_{{product.id}}'>
       <img ng-src='{{pic.url}}' />
    </div>
</div>

我想要做像this.But NG-的init ='_图片{{product.id}} 抛出一些exception.Any补救或替代的解决方案为这个?

I wanna do like this.But ng-init='images_{{product.id}}' is throwing some exception.Any remedy or alternate solution for this?

你正在做的可能还不如NG-INIT采取标准的JavaScript前pression让 {{}} 工作方式code>是行不通的。

The way you are doing may not work as ng-init take standard Javascript expression so {{}} would not work.

由于JavaScript对象只是一个key-value集合。您需要使用 [] 标记访问动态属性,而不是上符号。

Since javascript objects are just a key-value collection. You need to access dynamic properties using [] notation instead on . notation.

所以这

&LT; D​​IV NG-的init ='_图片{{product.id}} = product.images&GT;

变为

&LT; D​​IV NG-的init ='本[图片_+ this.product.id] = product.images'&GT;

请参阅拨弄我创建