[英]Angular 6 dynamic template reference variable
I have two users from which I need to take input and save partially, Here when I am chaging anything inside one child component both save buttons are enabled. 我有两个用户,我需要从中接受输入并部分保存,在这里,当我在一个子组件中追踪任何内容时,两个保存按钮都被启用。 How I can fix it?
我该如何解决?
Child Component 子组件
<div [ngModelGroup]="user" #fieldset="ngModelGroup">
<input name="first" [ngModel]="user.first" minlength="2">
<input name="last" [ngModel]="user.last" required>
<button [disabled]="fieldset.pristine||fieldset.invalid">
Save
</button>
</div>
Parent Component 父组件
<form>
<child-component [user]="object1"></child-component>
<child-component [user]="object2"></child-component>
</form>
Is it possible to template reference variable dynamic 是否可以模板引用变量动态
Note : Thanks benshabatnoam for your efforts. 注意 :感谢benshabatnoam的努力。
Parent-Component CODE
父组件代码
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
users = [
{first: 'first user first', last: 'first user last'},
{first: 'second user first', last: 'second user last'}
];
constructor() { }
ngOnInit() {
}
}
Parent-Component HTML
父组件HTML
// You don't have to iterate, you can have two or more if you like.
// <child-component [user]="users[0]"></child-component>
// <child-component [user]="users[1]"></child-component>
<ng-container *ngFor="let user of users">
<child-component [user]="user"></child-component>
</ng-container>
Child-Component CODE
子组件代码
import { Component, Input } from '@angular/core';
@Component({
selector: 'child-component',
templateUrl: './child.component.html'
})
export class ChildComponent {
@Input() user;
onSubmit(form){
if(form.invalid){
// Do stuff...
}
}
}
Child-Component HTML
子组件HTML
<form (ngSubmit)="onSubmit(myForm)" #myForm="ngForm">
<input [(ngModel)]="user.first" name="first" minlength="2">
<input [(ngModel)]="user.last" name="last" required>
<button type="submit" [disabled]="myForm.invalid">
Save
</button>
</form>
I provided the unique "ngModelGroup" for each child and my problem is resolved now 我为每个孩子提供了唯一的“ ngModelGroup”,现在我的问题已解决
Parent.component.html Parent.component.html
<form #f="ngForm">
<child-component [formGroupName]="'user1'" [user]="object1"></child-component>
<child-component [formGroupName]="'user2'" [user]="object2"></child-component>
</form>
<pre>{{f.value|json}} </pre>
Child.component.html Child.component.html
<div [ngModelGroup]="formGroupName" #fieldset="ngModelGroup">
<input name="first" [ngModel]="user.first" minlength="2">
<input name="last" [ngModel]="user.last" required>
<button [disabled]="fieldset.pristine||fieldset.invalid">
Save
</button>
</div>
Child.component.ts Child.component.ts
import { Component, Input } from '@angular/core';
import { ControlContainer, FormGroup, NgForm } from '@angular/forms';
@Component({
selector: 'child-component',
templateUrl: './child.component.html',
viewProviders: [{provide: ControlContainer, useExisting: NgForm}]
})
export class ChildComponent {
@Input() formGroupName : string;
@Input() user;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.