2017-09-19 93 views
0

如何允許重複ng-repeat跟蹤並更新控制器中的ng-repeat列表?重複ng-repeat跟蹤

通過使用軌道由$指數我不能夠更新範圍可變的,但我想允許重複的對象在NG-重複

條件: 1)允許重複在NG-重複 2)使用軌道通過從控制器 3)更新NG-重複列表

HTML

<div> 
    <ng-repeat="question in pagedata.questions track by $index"> 
      <p>{{question.questionText}}<p> 
     <div ng-repeat="option in question.optionList track by $index"> 
       <p ng-click="changeNextQuestion(question.nextQuestionId)">{{option}}</p> 
     </div> 

</div> 

控制器

$scope.pagedata.questions = [ 
    { 
     "questionId" : 1 
     "questionText" : "A" 
     "optionList" : [A1, A2, A3], 
     "nextQuestionId" : 2 

    }, 
    { 
     "questionId" : 2 
     "questionText" : "B" 
     "optionList" : [B1, B2, B3], 
     "nextQuestionId" : 2 
    }, 
{ 
     "questionId" : 3 
     "questionText" : "C" 
     "optionList" : [C1, C2, C3], 
     "nextQuestionId" : 2 
    } 
    ]; 
$scope.pagedata.questionsBack = angular.copy($scope.pagedata.questions); 

$scope.changeNextQuestion = function(nextQuestionId){ 
    var nextQuestion = findNextQuestionIndex($scope.pagedata.questions, nextQuestionId); 
    $scope.pagedata.questions[0] = $scope.pagedata.questionsBack[nextQuestion]; 
    }); 
//I want to update view with new value. 
} 


} 
+0

利用軌跡由$指數。更新只是更新範圍值。 –

回答

0

你檢查這one. 我希望這是給你很大的幫助。

在控制器

var studentsList = [ 
    {"Id": "101", "name": "siva"}, 
    {"Id": "101", "name": "siva"}, 
    {"Id": "102", "name": "hari"}, 
    {"Id": "103", "name": "rajesh"}, 
    {"Id": "103", "name": "rajesh"}, 
    {"Id": "104", "name": "ramesh"}, 
]; 

var uniqueStandards = UniqueArraybyId(studentsList ,"id"); 

function UniqueArraybyId(collection, keyname) { 
    var output = [], 
    keys = []; 

    angular.forEach(collection, function(item) { 
    var key = item[keyname]; 
    if(keys.indexOf(key) === -1) { 
     keys.push(key); 
     output.push(item); 
    } 
    }); 
    return output; 
}; 
+0

納克重複=「在對象的對象由$索引跟蹤」 當納克單擊=「pushDuplicateAndUpdateView()」 - >我要推複製對象列出和更新視圖 控制器{ $ scope.Objects [A ,B,C,B]; (){ $ scope.Objects [0] = C; } } – Rohit

+0

現在你可以檢查,我更新了我的答案。 –

+0

請給上述評論的解決方案 – Rohit