angularjs:想要锁定对象值,所以当ng-change无法重置对象值

时间:2016-03-15 07:44:02

标签: javascript angularjs angularjs-ng-change

问题:在ng-change指令中调用的方法有一个监听器,它监视输入(金额)并用输入更改更新vm.argiterraYearlyLocalCost对象:

//controller
    function getAgriAmount(year,amount) {
              if (typeof amount !== "number" ) {
                amount = parseInt(amount ||0);
              };
              vm.argiterraYearlyLocalCost[year] =  amount;
            }
//view
    <md-input-container flex>
        <label translate>PROJECT.WIZARD.BUDGET.AMOUNT</label>
        <input name="localCostAmount_{{year}}_{{$index}}"
               ng-model="localCostContributor.amount"
               ng-click="vm.showExplanation('LOCAL_COST_AMOUNT')"
               ng-change="vm.getAgriAmount(year,localCostContributor.amount)"
               ain-numbers-only
               type="text"
               required>
    </md-input-container>

//controller add the amount and containing in object
    function addLocalCostContributor(localCost, year) {
                localCost.contributors.push({});
                vm.argiterraYearlyLocalCost[year] = 0;

                for (var i = 0; i < localCost.contributors.length; i++) {
                    if (angular.isDefined(localCost.contributors[i].contributor)
                    && localCost.contributors[i].contributor.name === 'Agriterra') {
                        var amount = parseInt(localCost.contributors[i].amount);
                        vm.argiterraYearlyLocalCost[year] = parseInt(vm.argiterraYearlyLocalCost[year] || 0) + amount;
                    }
                }
            }

成功添加金额,当我点击添加按钮,但如果我想为年添加另一个金额 - 更改将其重置为当前金额类型,我想要的是ng-change将新金额添加到旧金额已添加的金额。我可以提供更多日志和信息如果事情不清楚。谢谢!

0 个答案:

没有答案