0

I'm creating a form in angular 2 and assign fields for FormBuilder:

ngOnInit() {
this.countryOfResidence = new FormControl(this.naturalPerson.countryOfResidence)
this.firstName = new FormControl(this.naturalPerson.firstName, [Validators.required, Validators.maxLength(30)])
this.middleName = new FormControl(this.naturalPerson.middleName, [Validators.required, Validators.maxLength(30)])
this.lastName = new FormControl(this.naturalPerson.lastName, [Validators.required, Validators.maxLength(60)])
this.NIN = new FormControl(this.naturalPerson.NIN)
this.countryOfBirth = new FormControl(this.naturalPerson.countryOfBirth)
this.birthDate = new FormControl(this.naturalPerson.birthDate)
this.citizenship = new FormControl(this.naturalPerson.citizenship)

this.primaryDataForm = this.fb.group({
  countryOfResidence: this.countryOfResidence,
  firstName: this.firstName,
  middleName: this.middleName,
  lastName: this.lastName,
  NIN: this.NIN,
  countryOfBirth: this.countryOfBirth,
  birthDate: this.birthDate,
  citizenship: this.citizenship
})
}

By this i refer to component fields. Is there a better syntax? Tried spread/rest operator:

let obj = ({
  countryOfResidence,
  firstName,
  middleName,
  lastName,
  NIN,
  countryOfBirth,
  birthDate,
  citizenship
} = this)

But it's not working... I'm pretty sure it can be simplified. How can I do that?

Edited: my package.json

{
  "dependencies": {
    "@angular/animations": "4.0.2",
    "@angular/common": "4.0.2",
    "@angular/compiler": "4.0.2",
    "@angular/compiler-cli": "4.0.2",
    "@angular/core": "4.0.2",
    "@angular/forms": "4.0.2",
    "@angular/http": "4.0.2",
    "@angular/platform-browser": "4.0.2",
    "@angular/platform-browser-dynamic": "4.0.2",
    "@angular/platform-server": "4.0.2",
    "@angular/router": "4.0.2",
    "angular-l10n": "^3.0.3",
    "bootstrap": "4.0.0-alpha.6",
    "core-js": "2.4.1",
    "rxjs": "5.1.0",
    "typescript": "2.2.2",
    "zone.js": "0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "6.0.60",
    "codelyzer": "2.0.0",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "3.2.0",
    "karma": "1.4.1",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "karma-coverage-istanbul-reporter": "0.2.0",
    "protractor": "5.1.0",
    "ts-node": "2.0.0",
    "tslint": "4.5.0",
    "typescript": "2.2.0"
  }
}
Jarosław Rewers
  • 1,059
  • 3
  • 14
  • 23

1 Answers1

0

I guess you're missing the concept of local variables, which the names are available only inside the function. See What is the scope of variables in JavaScript? for an explanation.

With ES6 object initialization shorthand, the code would be:

ngOnInit() {
  const countryOfResidence = new FormControl(this.naturalPerson.countryOfResidence)
  const firstName = new FormControl(this.naturalPerson.firstName, [Validators.required, Validators.maxLength(30)])
  const middleName = new FormControl(this.naturalPerson.middleName, [Validators.required, Validators.maxLength(30)])
  const lastName = new FormControl(this.naturalPerson.lastName, [Validators.required, Validators.maxLength(60)])
  const NIN = new FormControl(this.naturalPerson.NIN)
  const countryOfBirth = new FormControl(this.naturalPerson.countryOfBirth)
  const birthDate = new FormControl(this.naturalPerson.birthDate)
  const citizenship = new FormControl(this.naturalPerson.citizenship)

  this.primaryDataForm = this.fb.group({
    countryOfResidence,
    firstName,
    middleName,
    lastName,
    NIN,
    countryOfBirth,
    birthDate,
    citizenship
  })
}
Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41