【发布时间】:2017-03-05 19:45:22
【问题描述】:
我有一个包含对象的数组。我想从数字类型的输入框中更改此数组中的对象数。因此,每次输入框中的数字增加时,我都会增加长度,减少相同。我还想根据数组的增加或减少来推送默认对象或弹出最后一个对象。这是我当前的代码。
// html
<input type="number" class="form-control" min="0" max="12" ng-click ="topEntriesSelectOffense()" ng-value="page.offense.topRow.length">
//my controller
$scope.topEntriesSelectOffense = function () {
// push if number is increased
$scope.page.offense.topRow.push({
"1": {
"firstName": "FirstName",
"lastName": "LastName",
"headshot": "file/path"
},
"2": {
"firstName": "FirstName",
"lastName": "LastName",
"headshot": "file/path"
},
"3": {
"firstName": "FirstName",
"lastName": "LastName",
"headshot": "file/path"
},
"position": "POS"
});
// pop if the number is reduced
$scope.page.offense.topRow.pop();
}
问题是我无法真正判断用户何时减少了数字,并且我不太确定如何检测值的增加或减少。如果我对 page.offense.topRow.length 使用 ng-model,我将得到一个在最后一个索引之后没有对象的数组,因为它会增加长度,然后推动对象在数组中留下一个间隙。数组看起来像这个数组 = [1, 3]。谢谢!
【问题讨论】:
标签: javascript html angularjs arrays