设置输入的最大值时的插值错误

时间:2016-03-11 13:11:37

标签: javascript html angularjs

这里的问题是输入中的max="{{vm.maxAgriWomenBudget(year)}}"显示此错误Error: [$interpolate:interr] Can't interpolate: {{vm.maxAgriWomenBudget(year)}} TypeError: Cannot read property '2016' of undefined 2016是vm.maxAgriWomenBudget(year)内的(年)参数,它获取包含年(键)的对象及其value(应该是输入中的最大值),如果未定义year,则返回0。

vm.argiterraYearlyLocalCost = {}; // intialised at the top in controller
function maxAgriWomenBudget (year) {
        return angular.isDefined(vm.argiterraYearlyLocalCost[year]) ? vm.argiterraYearlyLocalCost[year]:0;
    }

在开始时vm.yearlyLocalCost[year]是空对象,但是当我添加东西时,对象就会填满。所以我做了函数来检查vm.yearlyLocalCost [year]是否未定义然后返回0。

<md-input-container flex>
                          <label translate>PROJECT.WIZARD.BUDGET.WOMEN_BUDGET_ARGITERRA</label>
                          <input name="genderBudgetAgriWomenBudgetYearly_{{year}}"
                                 ng-model="genderBudget.agriWomenBudgetYearly"
                                 placeholder="Budget spent on women reached of yearly cost contributed by Agriterra "
                                 ng-click="vm.showExplanation('ADVISE_BUDGET_AMOUNT')"
                                 ain-numbers-only
                                 max="{{(vm.yearlyLocalCost[year] || 0)}}"
                                 type="text">

 </md-input-container>

1 个答案:

答案 0 :(得分:1)

您正在初始化vm.argiterraYearlyLocalCost,但在下面的函数中引用vm.yearlyLocalCost。在两个地方使用相同的标识符,该错误应该消失。该错误是因为yearlyLocalCost不是vm的属性。