简体   繁体   English

使用angular.js在ng-repeat中获取错误

[英]Getting error in ng-repeat using angular.js

I am getting the below error while using ng-repeat in Angular.js. 在Angular.js中使用ng-repeat时出现以下错误。

Error: 错误:

Error: [ngRepeat:dupes] http://errors.angularjs.org/1.4.6/ngRepeat/dupes?p0=ses%20in%20sectionData&p1=string%3Al&p2=l
    at Error (native)
    at http://oditek.in/Gofasto/js/angularjs.js:6:416
    at http://oditek.in/Gofasto/js/angularjs.js:279:39
    at Object.fn (http://oditek.in/Gofasto/js/angularjs.js:129:128)
    at n.$digest (http://oditek.in/Gofasto/js/angularjs.js:130:206)
    at n.$apply (http://oditek.in/Gofasto/js/angularjs.js:133:236)
    at g (http://oditek.in/Gofasto/js/angularjs.js:87:376)
    at K (http://oditek.in/Gofasto/js/angularjs.js:91:448)
    at XMLHttpRequest.z.onload (http://oditek.in/Gofasto/js/angularjs.js:92:462)

I am explaining my code below. 我在下面解释我的代码。

<tbody id="detailsstockid">
<tr ng-repeat="ses in sectionData">
<td>{{$index+1}}</td>
<td>{{ses.colg_name}}</td>
<td>{{ses.session}}</td>
<td>
<!--<a ui-sref='dashboard.profile({p_i:profile.profile_id})'>
<input type='button' class='btn btn-xs btn-green' value='Edit' ng-click="reload();">  
</a>-->
<a ui-sref='dashboard.res.session'>
<input type='button' class='btn btn-xs btn-green' value='Edit' ng-click="editSessionData(ses.session_id);">  
</a>
</td>
<td>
<a ui-sref='dashboard.res.session'>
<input type='button' class='btn btn-xs btn-red' value='Remove' ng-click="deleteSessionData(ses.session_id);" >  
</a>
</td>
</tr>   
</tbody>

Here my requirement is while i will submit my form the data will suddenly display on the above list.Please check my below controller part. 这是我的要求,当我提交表单时,数据将突然显示在上面的列表中。请检查下面的控制器部分。

var userdata={'colg_name':$scope.colg_name.value,'session':$scope.session};
                $http({
                    method:'POST',
                    url:"php/resource/addSessionData.php",
                    data:userdata,
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).then(function successCallback(response){
                    alert(response.data['msg']);
                    $scope.colg_name.value='';
                    $scope.session=null;
                    $scope.sectionData.unshift(response.data);
                },function errorCallback(response) {
                    alert(response.data['msg']);
                    $state.go('dashboard.res.session',{}, { reload: true });
                });


$http({
        method:'GET',
        url:"php/resource/readSessionData.php",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        $scope.sessionData=response.data;
    },function errorCallback(response) {
    });

Here when user will inserting the data first time into DB after inserting the data should display on the list but here its throwing the below error after inserting into database. 当用户将数据插入后首次将数据插入数据库时​​,应该在列表中显示,但是在插入数据库后,它会引发以下错误。

Error-2: 错误2:

TypeError: $scope.sectionData.unshift is not a function
    at successCallback (resourceSessionController.js:48)
    at angularjs.js:118
    at n.$eval (angularjs.js:132)
    at n.$digest (angularjs.js:129)
    at n.$apply (angularjs.js:133)
    at g (angularjs.js:87)
    at K (angularjs.js:91)
    at XMLHttpRequest.z.onload (angularjs.js:92)

Please help me to resolve those above errors. 请帮助我解决以上错误。

It caused by the duplicate values in your data sectionData . 这是由数据sectionData的重复值引起的。 You can fix this issue by adding a track by with in your ng-repeat like below, 您可以通过在ng-repeat如下所示的轨道来解决此问题,如下所示,

<tr ng-repeat="ses in sectionData track by $index">

Update 更新资料

if($scope.sectionData != null && $scope.sectionData.length > 0){
    $scope.sectionData.unshift(response.data);
}
else{
    $scope.sectionData = response.data;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM