的输入框更改数组长度我有一个包含对象的数组。我想从类型数字的输入框中更改此数组中的对象数。所以每次输入框中的数字都增加了,我增加了长度,减少了相同的数字。我还想推送一个默认对象或弹出最后一个对象,具体取决于数组的增加还是减少。这是我目前的代码。从类型号为
// 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();
}
问题是我不能真正知道当用户减少数量,我不太确定如何检测值增加或减少。如果我使用ng-model作为page.offense.topRow.length,我会得到一个在最后一个索引处没有对象的数组,因为它会增加长度,然后推动对象在数组中留下空隙。该数组看起来像这个数组= [1,3]。谢谢!
我喜欢使用$ watch函数的想法,我一直试图让watch函数工作,但似乎并没有在每次输入框被改变的时候被解雇,这里是我的代码'$ scope。$看( 'topEntriesOffense',函数(NEWVALUE,属性oldValue){ 如果(NEWVALUE>属性oldValue){ topEntriesOffenseAdd();} } );' – inhaler
我需要,以帮助你更新的代码。 – Sagar
感谢所有的帮助,我使用了ng-change作为Soviut的建议,当它改变时,我根据模型的当前值调用函数creatObj()和destroyObj(),就像你建议的那样。我将旧值保存在一个范围变量中,并通过调用this.value在函数中检索当前值。我无法让手表正常工作。但这种方式工作得很好。感谢所有的帮助! – inhaler