我有一个问题。我无法使用Angular.js和PHP动态地将值设置到下拉列表中。
<tr ng-repeat="d in days">
<td>{{d.day}}</td>
<td>
<select class="form-control" id="catagory" ng-model="catagory" ng-options="cat.name for cat in listOfCatagory track by cat.value " ng-change="removeBorder('catagory',$index,catagory.value);" >
</select>
</td>
<td>
<select class="form-control" id="subcatagory[$index]" ng-model="subcatagory[$index]" ng-options="sub.name for sub in listOfSubCatagory[$index] track by sub.value " >
<option value="">Select Subcategory</option>
</select>
</td>
<td>
<input type="text" name="comment" id="comment" class="form-control oditek-form" placeholder="Add Comment" ng-model="comment" ng-keypress="clearField('comment');">
</td>
</tr>
当用户选择第一列下拉列表值时,相应的第二个下拉列表值将根据$ index动态设置。以下是我的控制器端代码。
$scope.removeBorder=function(id, index, catvalue) {
var catdata=$.param({'action' : 'subcat', 'cat_id' : catvalue});
$http({
method :'POST',
url : "php/customerInfo.php",
data : catdata,
headers : { 'Content-Type' : 'application/x-www-form-urlencoded' }
}).then(function successCallback(response) {
//console.log('sub', response.data);
angular.forEach(response.data, function(obj) {
var data = {'name' : obj.subcat_name, 'value' : obj.subcat_id};
$scope['listOfSubCatagory' + index] = data ;
})
}, function errorCallback(response) {
})
}
请帮我解决此问题。
答案 0 :(得分:1)
您在第二个下拉列表的ng-options中使用listOfSubCatagory [$ index]但在ajax响应中您将数据设置为$ scope ['listOfSubCatagory'+ index] = data;
因此,你不会将变量listOfSubCatagory作为数组,它将是listOfSubCatagory0,listOfSubCatagory1等等。这些变量将一次只保存一个选项而不是选项数组。
像这样修改你的控制器脚本并尝试一下。
$scope.listOfSubCatagory = [];
$scope.removeBorder=function(id,index,catvalue){
var catdata=$.param({'action':'subcat','cat_id':catvalue});
$scope.listOfSubCatagory[index] = [];
$http({
method:'POST',
url:"php/customerInfo.php",
data:catdata,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
//console.log('sub',response.data);
angular.forEach(response.data,function(obj){
var data={'name':obj.subcat_name,'value':obj.subcat_id};
$scope.listOfSubCatagory[index].push(data);
})
},function errorCallback(response) {
})
}
答案 1 :(得分:0)
按如下方式更改您的代码
$scope.removeBorder=function(id,index,catvalue){
var listOfSubCatagory=[];
var catdata=$.param({'action':'subcat','cat_id':catvalue});
$http({
method:'POST',
url:"php/customerInfo.php",
data:catdata,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
//console.log('sub',response.data);
angular.forEach(response.data,function(obj){
var data={'name':obj.subcat_name,'value':obj.subcat_id};
$scope.listOfSubCatagory.push(data) ;
})
},function errorCallback(response) {
})
}
希望这会对你有所帮助。
答案 2 :(得分:0)
<select class="form-control" ng-model="subcatagory" id="subcatagory">
<option value="">Select Subcategory</option>
<option ng-repeat="size in sizes" ng-attr-value="{{size.subcat_id}}">{{size.subcat_name}}
</option> </select>
$scope.removeBorder=function(id,index,catvalue){
var catdata=$.param({'action':'subcat','cat_id':catvalue});
$http({
method:'POST',
url:"php/customerInfo.php",
data:catdata,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
//console.log('sub',response.data);
$scope.sizes=response.data;
},function errorCallback(response) {
})
}