【问题标题】:Angular Reactive Forms角反应形式
【发布时间】:2017-06-08 00:03:58
【问题描述】:

我有两个 Angular 组件。一种称为网络服务,另一种称为网络服务。我在 Web 服务的模板页面上托管一个 Web 服务,并尝试通过我从 @angular/core 导入的 @Input() 装饰器绑定到它的服务属性。 However, whenever a web service is selected, I'm getting an error message of 'Cannot read property controls of undefined' which seems to be a problem with my web-service.component.html when it accesses inputParameters.controls.设置后,我已将 inputParameters.controls 打印到控制台,并且名称和类型都肯定作为表单控件存在。我是否以某种方式在 HTML 中错误地访问了它们?

web-services.component.ts

export class WebServicesComponent implements OnInit {
  services: Observable<WebService[]>;
  isLoading = false;
  selectedService: WebService;

  constructor(private webServicesService: WebServicesService) { }

  ngOnInit() {
    this.getServices();
  }

  getServices() {
    this.isLoading = true;
    this.services = this.webServicesService.getWebServices()
      .finally(() => this.isLoading = false);
    this.selectedService = undefined;
  }

  selected(service: WebService) {
    this.selectedService = service;
  }

}

web-services.component.html

<h3 *ngIf="isLoading"><i>Loading services ... </i></h3>
<h3 *ngIf="!isLoading && !selectedService">Select a service:</h3>

<ul class="ms-List" *ngIf="!selectedService">
    <li class="ms-ListItem" tabindex="0" *ngFor="let service of services | async">
        <span class="ms-ListItem-primaryText">{{service.name}}</span> 
        <span class="ms-ListItem-secondaryText">{{service.description}}</span> 
        <span class="ms-ListItem-tertiaryText">{{service.version}}</span> 
        <span class="ms-ListItem-metaText">{{service.creationTime}}</span> 
        <a class="ms-Link" (click)="selected(service)">Select</a> 
    </li>
</ul>

<div *ngIf="selectedService">
    <app-web-service [service]="selectedService"></app-web-service>
</div>

web-service.component.ts

export class WebServiceComponent implements OnInit {
  @Input() service: WebService;

  serviceForm: FormGroup;

  constructor(private fb: FormBuilder, private excelService: ExcelService) { 
    this.createForm();
  }

  ngOnInit() {

  }

  createForm() {
    this.serviceForm = this.fb.group({
      inputParameters: this.fb.array([]),
      outputParameters: this.fb.array([])
    });
  }

  ngOnChanges() {
    this.setParameters()
  }

  setParameters() {
    console.log(this.service.inputParameterDefinitions);
    const inputParameters = this.service.inputParameterDefinitions.map(input => this.fb.group(input));
    const inputParametersFormArray = this.fb.array(inputParameters);
    this.serviceForm.setControl('inputParameters', inputParametersFormArray);

    console.log(this.serviceForm.controls);

    const outputParameters = this.service.outputParameterDefinitions.map(output => this.fb.group(output));
    const outputParametersFormArray = this.fb.array(outputParameters);
    this.serviceForm.setControl('outputParameters', outputParametersFormArray);
  }

web-service.component.html

<form [formGroup]="serviceForm" (ngSubmit)="onSubmit()" novalidate>
    <!-- Service Controls -->
    <div formArrayName="inputParameters">
        <div *ngFor="let parameter of inputParameters.controls; let i=index" [formGroupName]="i">
            <!--The repeated parameter template-->
            <div>
                <label>Name:
                    <input formControlName="name">
                </label>
            </div>
            <div>
                <label>Type:
                    <input formControlName="type">
                </label>
            </div>
        </div>
    </div>
</form>

【问题讨论】:

    标签: angular properties binding


    【解决方案1】:

    在设置参数之前尝试测试你的service是否不为空:

    ngOnChanges(changes: {[ propName: string]: SimpleChange}) {
            if(changes[service].currentValue)
                  this.setParameters();
    }
    

    希望对你有帮助:)

    【讨论】:

    • 是的,我检查了服务,它不为空。它已成功通过属性绑定。我是否在 html 中错误地访问它?
    • 你可以试试:&lt;div *ngFor="let parameter of serviceForm.controls['inputParameters'].controls; let i=index" [formGroupName]="i"&gt;
    • 成功了!谢谢!当我已经声明 formGroup 是服务表单并且 formArrayName 是 inputParameters 时,为什么我需要重新访问 serviceForm?
    猜你喜欢
    • 2019-06-04
    • 2020-05-20
    • 2021-07-24
    • 1970-01-01
    • 2018-07-30
    • 2020-04-03
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多