javascript - Angular 数组而不是对象

标签 javascript arrays angularjs angular-ngmodel

我的 View 中有以下 HTML。当我注销该值时,在 ng-click 按钮上,该值是一个以数字作为键的对象,而不是一个数组。有谁知道我哪里出错了?

记录的是一个键为 0 的对象,可以这样访问:ObjectName.0

              <div class="EquipItems" ng-repeat="(key,value) in Day.equipment track by $index">
                   <label class="item item-input item-select">
                    <div class="input-label">
                        {{key}}
                    </div>
                    <select ng-model="value[$index].itemName" ng-options="item as item for item in Equipment.{{key}}">
                        <option value="">Select Option</option>
                    </select>
                    </label>
                    <label class="item item-input">
                        <input type="number" ng-model="value[$index].value" placeholder="Amount/Value">
                    </label>

                    <button class="button button-balanced button-outline button-block" ng-click="AddItem(value)">
                      Add {{key}}
                    </button>
               </div>

这是 Day 对象的结构。

{
                staff : [
                    {
                        name : '',
                        designation : '',
                        timeIn : '',
                        timeOut : ''
                    }
                ],
                vehicles : [
                    {
                        type : '',
                        distance : ''
                    }
                ],
                equipment : {
                    Staff : [
                        {
                            itemName : '',
                            value : ''
                        }
                    ],
                    Equipment : [
                        {
                            itemName : '',
                            value : ''
                        }
                    ],
                    Vehicles : [
                        {
                            itemName : '',
                            value : ''
                        }
                    ],
                    Absorbents : [
                        {
                            itemName : '',
                            value : ''
                        }
                    ],
                    PPE : [
                        {
                            itemName : '',
                            value : ''
                        }
                    ]

                },
                comment :  [
                    {
                        time : '',
                        comment : ''
                    }
                ],
                photos : [
                    {
                        src : '',
                        comment : ''
                    }
                ]
            }

最佳答案

只需在此处创建一个演示:JSFIDDLE

在功能添加项目中,我输入:

 $scope.AddItem = function(value){
    console.log(value);  // [Object]
    console.log(typeof(value)); //object
    console.log(Object.prototype.toString.apply(value)); //[object Array]  still an array
    console.log(value[0]); // get the object
};

value 仍然是一个数组。数组中的元素是Object。喜欢:

{
    itemName : '',
    value : ''
}

关于javascript - Angular 数组而不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859533/

相关文章:

javascript - 意外 token : u JSON. parse() 问题

javascript - 使用 React 和 API 时如何修复 "TypeError: items.map is not a function"?

javascript - Jquery .text().length 计算元素的代码和字符?

python - 我怎样才能对每 n 个数组值求和并将结果放入一个新数组中?

c# - 根据相同字符串在列表中出现的频率使用 C# 对列表进行排序

javascript - 调试时出现 404 Not Found,但 URL 在浏览器中有效

angularjs - Angular : can't set initial value to input box

javascript - 解构作业

javascript - Lodash orderby descending 在 JavaScript 中不起作用(React Native)

php - Codeigniter session 不起作用