AngularJS - 更新父级的Repeater项

时间:2014-01-28 21:41:39

标签: angularjs angularjs-scope angularjs-ng-repeat

我有一个包含4个答案的问题。答案在带有ng-click绑定的转发器中,将答案设置为answers = true,但我也想将父问题设置为answers = true。

$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

谢谢, 斯科特

1 个答案:

答案 0 :(得分:0)

你似乎使它变得比它应该复杂得多。只需使用

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

并将registerAnswer函数定义为

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