【问题标题】:Getting error while setting Database value in drop down list using Angular.js使用 Angular.js 在下拉列表中设置数据库值时出错
【发布时间】:2016-02-25 13:41:39
【问题描述】:

我在尝试使用 Angular.js 设置数据库值时遇到以下错误。

错误:

TypeError: index.push is not a function

我在下面解释我的代码。

<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>

当用户从第一个下拉列表中选择值时,下面的部分正在执行。

$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.push(data);
            })
        },function errorCallback(response) {
        })
    }

我在$scope.listOfSubCatagory+index.push(data); 行遇到错误。请帮我解决这个错误。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    您正在使用 ng-change="removeBorder('category',$index,catagory.value);"

    其中 $index 返回列表中元素的索引,它是一个整数。

    您试图在整数上调用 .push() 函数,这就是您收到此错误的原因。

    【讨论】:

    • 我需要将$scope.listOfSubCatagory+index 转换为$scope.listOfSubCatagory0,$scope.listOfSubCatagory1..etc..
    • @p2:我按照你的方法做了,但同样的错误出现在同一行。
    • 不要做 index.push() , index 是一个整数,整数没有 push 功能。 push 函数适用于数组对象。我没有你的代码,所以我无法正确调试你的问题。尝试处理 index.push() 部分。
    • 我已经给出了代码。请检查我的帖子视图部分是否已经存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多