我需要使用应使用Angular.js动态设置的模型名称来获取数据。这是我的代码:
$scope.saveResturantDetails=function(billdata){
console.log('catagory',$scope.answers['catagory0'].value);
for(var i=0;i<$scope.days.length;i++){
var cat="'"+"catagory'"+i+"'";
//var cat='catagory0';
var subcat="'"+"subcatagory"+i+"'";
console.log('cat id',cat);
var data={'cata':$scope.answers[cat].value,'subcat':$scope.answers[subcat].value,'comment':$scope.answers["'comment"+i+"'"]};
arr.push(data);
}
console.log('arr',arr);
}
但是我收到以下错误:
TypeError: Cannot read property 'value' of undefined
但是当我按照这个var cat='catagory0';
设置模型名称时,它会正常运行。
下表给出了表格。
<tr ng-repeat="d in days">
<td>{{d.day_name}}</td>
<td> <select class="form-control" name="catagory[$index]" id="{{'catagory'+$index}}" ng-model="answers['catagory'+$index]" ng-options="cat.name for cat in listOfCatagory track by cat.value " ng-change="removeBorder('catagory',$index,answers['catagory'+$index].value);" >
</select></td>
<td>
<select class="form-control" name="subcatagory[$index]" id="{{'subcatagory'+$index}}" ng-model="answers['subcatagory'+$index]" ng-options="sub.name for sub in listOfSubCatagory[$index] track by sub.value " ng-change="setSubCatagory($index,subcatagory[$index].value);" >
<option value="">Select Subcategory</option>
</select>
</td>
<td><input type="text" name="comment[$index]" id="{{'comment'+$index}}" class="form-control oditek-form" placeholder="Add Comment" ng-model="answers['comment'+$index]" ng-keyup="comment($index,comment[$index]);"></td>
</tr>
答案 0 :(得分:0)
您定义了错误的变量:
scope.saveResturantDetails=function(billdata){
for(var i=0;i<$scope.days.length;i++){
console.log($scope.days[i].subcatagory); // should print something
console.log($scope.days[i].catagory); // should print something
var data={'cata':$scope.answers[$scope.days[i].catagory].value,'subcat':$scope.answers.[$scope.days[i].subcatagory].value,'comment':$scope.answers["'comment"+i+"'"]};
arr.push(data);
}
console.log('arr',arr);
}