gpt4 book ai didi

javascript - 无法使用 Angular.js 动态设置值

转载 作者:行者123 更新时间:2023-12-03 07:35:48 25 4
gpt4 key购买 nike

我有一个问题。我无法使用 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 动态设置。下面是我的 Controller 端代码。

$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) {
})
}

请帮我解决这个问题。

最佳答案

您在第二个下拉列表的 ng-options 中使用 listOfSubCatagory[$index],但在 ajax 响应中您将数据设置为 $scope['listOfSubCatagory'+index] = data ;

因此,您不会将变量 listOfSubCatagory 作为数组获得,它将是 listOfSubCatagory0、listOfSubCatagory1 等。这些变量将一次保存一个选项,而不是选项数组。

像这样修改你的 Controller 脚本并尝试一下。

$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) {
})
}

关于javascript - 无法使用 Angular.js 动态设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com