【问题标题】:AngularJS 1: How to bind index as expression to an array in html?AngularJS 1:如何将索引作为表达式绑定到 html 中的数组?
【发布时间】:2015-11-10 17:27:21
【问题描述】:

我有一点问题。我正在尝试使用 ng-repeat 来显示一些数据:

ng-repeat="color in apples[{{index}}].color"

在哪里

$scope.index = $stateParams.index;

但是,我收到一个语法错误。

在这个例子中动态添加索引到数组的正确方法是什么?

【问题讨论】:

    标签: arrays angularjs ng-repeat


    【解决方案1】:

    只需使用循环索引:

    ng-repeat="color in apples[$index].color"
    

    ng-repeat="color in apples[index].color"
    

    【讨论】:

    • 这对我不起作用:当我尝试在 ng-repeat 下方显示颜色值时,我有一个空对象。 (我将值显示为 {{color}})
    • 在 HTML 代码中我有:
    • 错误信息是什么?你能创造一个小提琴吗?
    • 好吧,这行得通 :) 我不得不将 index 重命名为 pp.index,因为我使用控制器作为语法 :)
    • 您只能在可迭代的项目上使用 ng-repeat。检查 apples[index] 是否有任何属性。还分享完整的代码。很难理解 apples 是数组还是对象
    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2018-08-17
    • 2016-06-02
    相关资源
    最近更新 更多