繁体   English   中英

如何将FormArray传递给其他组件

[英]How to pass formarray to other component

我想将formarray传递给子组件以在其中显示formarray的值。 下面是代码,到目前为止我已经尝试过了。 我找不到在子组件中显示formarray值的方法。

app.component.html

<div [formGroup]="userForm">
  <div formArrayName="users">
    <div *ngFor="let user of users.controls; let i = index">
      <input type="text" placeholder="Enter a Room Name" [formControlName]="i">
    </div>
  </div>
</div>
<button (click)="addUser()">Add Room</button>
<title [users]="users"></title>

app.component.ts

userForm: FormGroup;

constructor(private fb: FormBuilder) {}

public get users(): any {
  return this.userForm.get('users') as FormArray;
}

ngOnInit() {
  this.userForm = this.fb.group({
    users: this.fb.array([this.fb.control('')])
  });
}

addUser() {
  this.users.push(this.fb.control(''));
}

title.component.html

<div *ngFor="let user of users.controls">{{ user.value }}</div>

title.component.ts

@Input() users;

ngOnChanges(changes) {
  console.log(changes);
}

但以上代码未在子组件中显示formarray值。

示例stackblitz在这里

title是一个关键字,它已经定义了HTML标签。 只是使用不同的名称作为组件selector

 selector: 'title1',

STACKBLITZ DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM