【问题标题】:set multiple scope value From ng-model从 ng-model 设置多个范围值
【发布时间】:2016-02-18 10:34:25
【问题描述】:

我有这样的范围对象:

myObject: {
  A: '',
  B: '',
  otherValues: {
    0: {
       myValue: '',
       status: ''
       }
    1: {
       myValue: '',
       status: ''
       }
  }
}

我有复选框列表:

<tr ng-repeat="(key, value) in myArray">
<td><input name="{{value.myValue}}" type="checkbox" 
    ng-model="myObject.otherValues[$index].status" 
    ng-click="setFunction(value.myValue,$index)" />
</tr>

状态由 ng-model (true,false) 设置。我想使用 setFunction 设置 myValue。 在我的控制器中,我有这个功能:

function setFunction (value,index) {
    $scope.myObject.otherValues[index].myValue= value;
}

但是会产生控制台错误:TypeError: Cannot read property '0' of undefined 我该如何解决?

【问题讨论】:

  • 你能给我们一些小提琴或小玩意儿吗?
  • 我认为otherValues 应该是一个数组

标签: angularjs


【解决方案1】:

您的 Json 对象缺少逗号:

myObject: {
  A: '',
  B: '',
  otherValues: {
    0: {
       myValue: '',
       status: ''
       },  //Missing Comma
    1: {
       myValue: '',
       status: ''
       }
  }
}

【讨论】:

  • 是的,抱歉,但问题不是逗号……这是我的剪切/粘贴错误
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2016-12-24
  • 1970-01-01
相关资源
最近更新 更多