2014-01-28 57 views
0

我有一個問題,其中包含4個答案。答案在一個帶有ng-click綁定的轉發器中,答案設置爲回答= true,但我也希望將父問題設置爲回答= true。AngularJS - 直放站項​​目更新父母

$scope.questions = [{ 
    "text": "1111What do you think of this asdf asdf asdf asdf sdf asdf as?", 
    "answers":[ 
     {text:"answer 1", answered:false}, 
     {text:"answer 2", answered:false}, 
     {text:"answer 3", answered:false}, 
     {text:"answer 4", answered:false}], 
    "answered": false 
},{ 
    "text": "22What do you think of this asdf asdf asdf asdf sdf asdf as?", 
    "answers":[ 
     {text:"answer 21", answered:false}, 
     {text:"answer 22", answered:false}, 
     {text:"answer 23", answered:false}, 
     {text:"answer 24", answered:false}], 
    "answered": false 
}]; 
$scope.registerAnswer = function(){ 
    if (this.answer) { 
     this.answer.answered = true; 
     this.answer.$parent.$parent.answered = true; 
    } 
}; 

// html:: 
<button ng-click=registerAnswer()></button> 

我該如何讓孩子更新它的父母?

http://jsfiddle.net/scott_in_ct/VnQqH/197/

http://plnkr.co/edit/KVjA64JaopofX5KuKXPr

感謝, 斯科特

回答

0

你似乎使它更復雜的比它應該是。只需使用

ng-click="registerAnswer(answer, question)" 

和定義registerAnswer功能

$scope.registerAnswer = function(answer, question) { 
    answer.answered = true; 
    question.answered = true; 
} 
+0

衛生署!感謝那。 – Scott