我正在努力应对下一个场景
数据:
$scope.MyItem = [
{
"__v": 0,
"myItemId": "55ed819caefe18e81ffbd2d2",
"itemId": "56fec8abb192c870117ed393",
"january": 1,
"february": 1,
"march": 1,
"april": 1,
"may": 1,
"june": 1,
"july": 1,
"august": 1,
"september": 1,
"october": 1,
"november": 1,
"december": 1,
"_id": "56fec8abb192c870117ed394",
"itemName": "apple"
},
{
"__v": 0,
"myItemId": "55ed819caefe18e81ffbd2d2",
"itemId": "56fec8bfb192c870117ed395",
"january": 1,
"february": 1,
"march": 1,
"april": 1,
"may": 1,
"june": 1,
"july": 1,
"august": 1,
"september": 1,
"october": 1,
"november": 1,
"december": 1,
"_id": "56fec8bfb192c870117ed396",
"itemName": "other"
}
];
myapp.monthName = [
{text: 'january'},
{text: 'february'},
{text: 'march'},
{text: 'april'},
{text: 'may'},
{text: 'june'},
{text: 'july'},
{text: 'august'},
{text: 'september'},
{text: 'october'},
{text: 'november'},
{text: 'december'}
];
这是我的表
<form class="form-inline" ng-submit="myapp.updateMyItems()" >
<tbody>
<tr ng-repeat="item in MyItem">
<td>
{{item.itemName}}
</td>
<td ng-repeat="monthName in myapp.monthName">
<input type="number"
ng-model="MyItem[item._id][monthName.text]"
value="{{item[monthName.text]}}"
>
</td>
</tr>
</tbody>
<button type="submit">Save</button>
</form>
该表显示2行,每行12列,每行值1,设置第一行的第一个学期值为2,第二行为最后一个学期,值为2,并提交表格我得到下一个对象: / p>
myapp.updateMyItems = function() {
var countryItem = $scope.countryItem;
console.log(countryItem); // output:
// [Object, Object, 56fec8abb192c870117ed394: Object, 56fec8bfb192c870117ed396: Object]
//
// where Object, Object have the same values of "$scope.MyItem" and
//
// 56fec8abb192c870117ed394 has
// april : 2
// february : 2
// january : 2
// july : 1
// june : 2
// march : 2
// may : 2
// ... remaining the last semester exception the july with 1
// 56fec8bfb192c870117ed396 has
// august : 2
// december : 2
// february : 1
// july : 2
// november : 2
// october : 2
// september : 2
// and here the opposite situation from the last object
};
所以结果不是我所期望的。我在Angularjs做错了什么?
答案 0 :(得分:1)
您正在以错误的方式使用输入中的模型,您可以通过ng-model="item[monthName.text]"
引用它。检查我的例子: