【问题标题】:Copy some properties from one object to another将一些属性从一个对象复制到另一个对象
【发布时间】:2017-04-20 09:31:53
【问题描述】:

我正在用 Angular 2 创建一个表单并为 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
})
}

这里我指的是组件字段。有更好的语法吗?尝试传播/休息运算符:

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

但它不起作用......我很确定它可以简化。我该怎么做?

已编辑:我的 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"
  }
}

【问题讨论】:

  • 你知道通过letvarconst的局部变量吗?
  • @TatsuyukiIshi 我不太明白,你能详细说明一下吗?

标签: angular ecmascript-6


【解决方案1】:

我猜你错过了局部变量的概念,它的名称只能在函数内部使用。请参阅What is the scope of variables in JavaScript? 了解说明。

使用 ES6 对象初始化简写,代码如下:

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
  })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-08
    • 2016-12-21
    • 2018-02-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多