【问题标题】:angularjs how to push a value to specific position of arrayangularjs如何将值推送到数组的特定位置
【发布时间】:2014-01-20 08:55:25
【问题描述】:

我有一个从数组中获取值的选择下拉菜单。如果条件为真,我想将一个值推送到数组的第一个位置。

<select name="child_age">
    <option ng-repeat="child_age in age">{{child_age.childCount}}</option>
</select>

儿童年龄数组 $scope.age = [{childCount:'1 Yr'},{childCount:'2 Yrs'}];

我想将跟随值推到第一个位置。 $scope.age.push({ childCount: '孩子 1 岁' })

但它已被添加到最后一个位置。

我也尝试了以下代码

$scope.age.splice(0,0,"Child 1 Age")

它显示了位置。但不显示值

谁能帮我解决这个问题?谢谢

【问题讨论】:

    标签: arrays angularjs array-splice


    【解决方案1】:

    不要使用.push,而是使用.unshift

    我还建议使用ng-options 而不是自己使用ng-repeat,但在这种情况下这不会产生功能上的差异。

    【讨论】:

      【解决方案2】:

      您正在尝试推送错误的数据格式。它应该插入为

      $scope.age.splice(0,0,{childCount:"Child 1 Age"})
      

      【讨论】:

        猜你喜欢
        • 2012-02-03
        • 1970-01-01
        • 2020-05-12
        • 2016-11-18
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多