Skip to content

关于questionnaire-item.component的ngOnChanges #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
clfeng opened this issue Jun 21, 2017 · 5 comments
Open

关于questionnaire-item.component的ngOnChanges #13

clfeng opened this issue Jun 21, 2017 · 5 comments

Comments

@clfeng
Copy link

clfeng commented Jun 21, 2017

github上代码

  ngOnChanges(changes: SimpleChanges){
      let questionnaireChange = changes['questionnaire'];
      if(questionnaireChange.previousValue.state && 
            questionnaireChange.currentValue.state !== questionnaireChange.previousValue.state){
          this.questionnaire = changes['questionnaire'].currentValue;
          this.setState();
      }
  }

官网PAI


class SimpleChange {
constructor(previousValue: any, currentValue: any, firstChange: boolean)
previousValue : any
currentValue : any
firstChange : boolean
isFirstChange() : boolean
}

通过changes为SimpleChanges的一个对象实例,那么应该不具有questionnaire方法吧?表示那不大理解,像老师请教一下。

@clfeng clfeng closed this as completed Jun 21, 2017
@clfeng clfeng reopened this Jun 21, 2017
@ghost
Copy link

ghost commented Jun 22, 2017

changes 是一个 SimpleChanges 类型对象,changes 里面有 questionnaire 这个 SimpleChange 类型的子对象,注意"s"结尾的区别。
questionnaire 是一个对象,不是一个方法

@clfeng
Copy link
Author

clfeng commented Jun 22, 2017

老师说还没注意到有没‘s’,谢谢啦。然后后来自己也debugger了一下,现在比较明白了。然后里面的github的代码存在着这么一个bug:

  ngOnChanges(changes: SimpleChanges){
      let questionnaireChange = changes['questionnaire'];
      if( questionnaireChange.previousValue.state && 
            questionnaireChange.currentValue.state !== questionnaireChange.previousValue.state){
          this.questionnaire = changes['questionnaire'].currentValue;
          this.setState();
      }
  }

这段代码中,当第一次调用到ngOnChanges的时候questionnaireChange.previousValue为undefined,所以调用questionnaireChange.previousValue.state会报错,感觉原本的代码意图应该是

if( questionnaireChange.previousValue&& 
            questionnaireChange.currentValue.state !== questionnaireChange.previousValue.state)

还有一点想请教下。changes所拥有对象的名称是根据@input来判断的?理由是OnChanges是用来在@input的值变化时调用的钩子?不知怎么理解对不对

@ghost
Copy link

ghost commented Jun 22, 2017

恩,谢谢反馈问题。

@ghost
Copy link

ghost commented Jun 22, 2017

OnChanges的参数值是根据@input定义的,当@input() 修饰的变量发生变化就会触发 OnChanges钩子

@clfeng
Copy link
Author

clfeng commented Jun 22, 2017

想明白了。谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant