Value: TypeError: Cannot read property 'arr' of undefined(…) TypeError: Cannot read property 'arr' of undefined

Value: TypeError: Cannot read property 'arr' of undefined(…) TypeError: Cannot read property 'arr' of undefined

您好,我正在制作多个表单验证,但遇到错误 'TypeError: 无法读取未定义的 属性 'forEach' '

我不知道该怎么办。 这是我的

app.component.ts

import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import { FormGroup, FormControl, FormBuilder, Validators, FormArray } from '@angular/forms';
import 'rxjs/add/operator/count';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public employees: FirebaseListObservable<any>;
  public updateForm: FormGroup;
  public formArray = new FormArray([]);
  employeesCount: number;
  angular: any;

  constructor(af: AngularFire) {
    this.employees = af.database.list('/employees');
  }
  addEmployee( firstname: string, lastname:string, age:number , sex: string) {
    this.employees.push({ firstname: firstname, lastname: lastname, age: age, sex:sex});
  }
  updateEmployee(key: string, firstname: string, lastname:string, age:number , sex: string) {
    this.employees.update(key, { firstname: firstname, lastname: lastname, age: age, sex:sex } );
  }
  deleteEmployee(key: string) {    
    this.employees.remove(key); 
  }

  ngOnInit() {
    this.angular.forEach(this.employees, function(value, key) {
      this.formArray.push(
        new FormGroup({firstname: new FormControl([Validators.required])})
      );
      console.log(value);
    });
    this.updateForm = new FormGroup(
      {firstname: new FormControl([Validators.required])}
      );

  }
}

以上问题由this.employees.forEach(function(employee){...})

解决

我在与 Unhandled Promise rejection: Cannot read property 'arr' of undefined ; Zone: <root> ; Task: WebSocket.addEventListener:message ; Value: TypeError: Cannot read property 'arr' of undefined(…) TypeError: Cannot read property 'arr' of undefined

相同的代码中遇到另一个问题

我怎么努力都不行,我不知道为什么?

export class AppComponent {
  public employees: FirebaseListObservable<any>;
  public updateForm: FormGroup;
  public arr = new FormArray([]);

  constructor(af: AngularFire) {
    this.employees = af.database.list('/employees');
  }
  addEmployee( firstname: string, lastname:string, age:number , sex: string) {
    this.employees.push({ firstname: firstname, lastname: lastname, age: age, sex:sex});
  }
  updateEmployee(key: string, firstname: string, lastname:string, age:number , sex: string) {
    this.employees.update(key, { firstname: firstname, lastname: lastname, age: age, sex:sex } );
  }
  deleteEmployee(key: string) {    
    this.employees.remove(key); 
  }

  ngOnInit() { 
    this.employees.forEach(function(e) {
      this.arr.push(new FormGroup({firstname: new FormControl([Validators.required])}));
      console.log(this.arr);
    });

  }

您似乎从未为 angular 属性 赋值,并确保 angular 属性 值应该是数组。我考虑过对象它不起作用

[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach][1]

你可以打电话this.employees.forEach()。您似乎正在尝试使用 AngularJS(版本 1)方法 angular.forEach(),这在 Angular 2.

中不是必需的

可以删除字段 angular,因为它未用于任何用途。