I have an issue.I can not set value into drop down list dynamically using Angular.js and 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>
When user will select first column drop down list value the respective second drop down list value will set dynamically as per $index.the below is my controller side code.
$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) {
})
}
Please help me to resolve this issue.
You are using listOfSubCatagory[$index] in ng-options of second dropdown but in the ajax response you are setting data to $scope['listOfSubCatagory'+index] = data ;
Hence, You will not get the variable listOfSubCatagory as array, it will be listOfSubCatagory0, listOfSubCatagory1, etc.. and these variables will hold a single option at a time instead of array of options.
Modify your controller script like this and give a try.
$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) {
})
}
Change your code as follows
$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) {
})
}
Hope this will help you.
<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) {
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.