Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度类型脚本:赋值后变量内容未定义_Angular_Typescript_Forms_Api_Http - Fatal编程技术网

Angular 角度类型脚本:赋值后变量内容未定义

Angular 角度类型脚本:赋值后变量内容未定义,angular,typescript,forms,api,http,Angular,Typescript,Forms,Api,Http,在我的ngOnInit方法中,我调用api获取用户信息,并将其设置为currentUser变量,同时尝试将currentUser的内容修补到我获得的from组中: this.currentUser is undefined .ts文件 export class ProfileComponent implements OnInit { currentUser: any; user: FormGroup; firstName: any; lastNa

在我的ngOnInit方法中,我调用api获取用户信息,并将其设置为currentUser变量,同时尝试将currentUser的内容修补到我获得的from组中:

this.currentUser is undefined
.ts文件

export class ProfileComponent implements OnInit {
      currentUser: any;
      user: FormGroup;
      firstName: any;
      lastName: any;
      cities: any[];
      fileToUpload: any;
      avatarPath: any;
      avatarURL: any;
      returnUrl: any;

      counts: any;

      constructor(
        private http: HttpClient,
        private router: Router,
        private toastr: ToastrService,
        private route: ActivatedRoute,
        private fb: FormBuilder
      ) {
        this.createForm();
      }

      createForm() {
        this.user = this.fb.group({

          firstName: new FormControl('', [Validators.required, Validators.minLength(3), Validators.pattern('^[a-zA-Z \-\']+')]),
          lastName: new FormControl('', [Validators.required, Validators.minLength(3), Validators.pattern('^[a-zA-Z \-\']+')]),
          email: new FormControl('', [Validators.email]),
          gender: new FormControl('', [Validators.required]),
          birthDate: new FormControl(''),
          phone: new FormControl({ value: '', disabled: true }, [Validators.required]),
          oldPassword: new FormControl(''),
          password: new FormControl(''),
          profession: new FormControl('', [Validators.pattern('^[a-zA-Z \-\']+')]),
          address: new FormControl('', [Validators.required]),
          city: new FormControl({ value: '', disabled: true }),
          postalCode: new FormControl('', [Validators.required, Validators.min(1000), Validators.max(10000)])
        }, )

      }


      ngOnInit() {    
        this.http.get('api/user').subscribe(user => {
          this.currentUser = user;
          localStorage.removeItem('currentUser');
          localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
        });
        this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
        this.http.get('api/cities').subscribe((cities: any[]) => {
          this.cities = cities;
        });
        this.user.patchValue({
          firstName: this.currentUser.firstName,
          lastName: this.currentUser.lastName,
          email: this.currentUser.email,
          profession: this.currentUser.profession,
          gender: this.currentUser.gender,
          birthDate: this.currentUser.birthDate,
          phone: this.currentUser.phone,
          oldPassword: this.currentUser.oldPassword,
          password: this.currentUser.newPassword,
          address: this.currentUser.address,
          postalCode: this.currentUser.postalCode,
          city: this.currentUser.city._id
        });
       }

     }
如何修复该问题并将我的可变内容修补到用户表单组中?
即使我试图在http get之外记录currentUser的内容,它也没有定义

您有异步问题。此.user.patchValue在get请求之前起作用。您可以将此部分纳入get方法

 this.http.get('api/user').subscribe(user => {
       this.currentUser = user;
       localStorage.removeItem('currentUser');
       localStorage.setItem('currentUser', JSON.stringify(this.currentUser));
       this.user.patchValue({
          firstName: this.currentUser.firstName,
          lastName: this.currentUser.lastName,
          email: this.currentUser.email,
          profession: this.currentUser.profession,
          gender: this.currentUser.gender,
          birthDate: this.currentUser.birthDate,
          phone: this.currentUser.phone,
          oldPassword: this.currentUser.oldPassword,
          password: this.currentUser.newPassword,
          address: this.currentUser.address,
          postalCode: this.currentUser.postalCode,
          city: this.currentUser.city._id
        });
    });

感谢您的快速回答,您能解释一下这个异步pbm的概念以及如何处理和使用它吗?对api的请求是异步工作的。这意味着它将继续,但下一步代码不会等待它。因此,在从api获取数据之前,this.user.patchValue尝试填充其数据。因此,要处理此问题,您可以在Subscribe方法中对api中的数据进行下一步处理。因为那里总是有数据。我建议您也使用if条件,即您从api和服务获取数据