代码之家  ›  专栏  ›  技术社区  ›  rhysclay

如何在初始加载后调用角度窗体生成器值更改

  •  0
  • rhysclay  · 技术社区  · 7 年前

    this.profileFormStageOne.get('nationality').valueChanges.subscribe(val => {
          if (val && val === 'Other') {
            let alert = this.alertCtrl.create({
              title: 'Your Nationality',
              inputs: [
                {
                  name: 'nationality',
                  placeholder: 'Your Nationality e.g. Brazilian',
                },
              ],
              buttons: [
                {
                  text: 'Update Nationality',
                  handler: data => {
                    this.manualNationality = data.nationality;
                    console.log(this.manualNationality);
                  },
                },
              ],
            });
            alert.present();
          }
        });
    

    不过,我也使用ngOnChanges()来修补以下形式的值:

      /**
       * Get initial form values
       * Needs to run from ngOnChanges because User in an input & is not ready in constructor
       */
      ngOnChanges() {
        if (this.User) {
          const profile: Profile = this.User.profile;
    
          this.profileFormStageOne.patchValue({
            nationality: profile.nationality,
    

    问题是,如果国籍字段已设置为“其他”,则在初始呈现(在修补程序值期间)上调用此函数。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Pavan Bahuguni    7 年前

    你可以订阅这样的国籍变更

         this
          .profileFormStageOne.controls['nationality'].valueChanges.subscribe(()=> {
       // your login
     })
    
        2
  •  0
  •   xkeshav    7 年前

    get nationality() {
           this.profileFormStageOne.get('nationality');
     }
    

     this.nationalityChange = this.nationality.valueChanges.subscribe((value: string) => {
                if (value === 'other') {
                  // do your requirement
                }
                this.nationality.updateValueAndValidity();
            });
    

    pathValue() 方法设置默认值 ngAfterViewInit() 钩子

    ngOnDestroy() {
            this.nationalityChange.unsubscribe();
        }