Angular 2 子组件中的 ngModel 更新父组件 属性
Angular 2 ngModel in child component updates parent component property
我做了一个简单的 UI,它包含两个组件(父组件和子组件)。
UI 的作用是当我在子组件的输入框中键入一些内容时。该值将使用 ngModel 更改。
这样子组件就可以正常工作了。
// Child Component
@Component({
selector: 'child',
template: `
<p>{{sharedVar}}</p>
<input [(ngModel)]="sharedVar">
`
})
export class ChildComponent {
sharedVar: string;
}
现在我有一个父组件,我打算使用与子组件相同的值。
我将子组件添加到父模板中,并使用依赖注入调用子组件的sharedVar
。
// Parent Component
@Component({
selector: 'parent',
template: `
<h1>{{sharedVar}}</h1>
<child></child>
`,
directives: [ChildComponent],
providers: [ChildCompnent]
})
export class ParentComponent {
sharedVar: string;
constructor(child: ChildComponent) {
this.sharedVar = child.sharedVar;
}
}
问题是当我在输入框中输入时,<p>
中的值会自动更改,而父组件的 <h1>
中的值不会更改。
您可以设置从子级到父级的事件发射器通信 (outputs
)。例如像这样:
@Component({
selector: 'child',
template: `
<p>Child: {{sharedVar}}</p>
<input [(ngModel)]="sharedVar" (ngModelChange)="change()">
`
})
export class ChildComponent {
@Output() onChange = new EventEmitter();
sharedVar: string;
change() {
this.onChange.emit({value: this.sharedVar});
}
}
和父组件:
@Component({
selector: 'parent',
template: `
<h1>{{sharedVar}}</h1>
<child (onChange)="sharedVar = $event.value"></child>
`,
directives: [ChildComponent]
})
export class ParentComponent {
sharedVar: string;
constructor() {
}
}
我们可以在 parent 模板中使用 [(x)]
语法来实现 two-way 与 child 的数据绑定。如果我们创建一个名为 xChange
的输出 属性,Angular 将自动更新 parent 属性。每当 child 更改值时,我们确实需要 emit()
一个事件:
import {Component, EventEmitter, Input, Output} from 'angular2/core'
@Component({
selector: 'child',
template: `
<p>Child sharedVar: {{sharedVar}}</p>
<input [ngModel]="sharedVar" (ngModelChange)="change($event)">
`
})
export class ChildComponent {
@Input() sharedVar: string;
@Output() sharedVarChange = new EventEmitter();
change(newValue) {
console.log('newvalue', newValue)
this.sharedVar = newValue;
this.sharedVarChange.emit(newValue);
}
}
@Component({
selector: 'parent',
template: `
<div>Parent sharedVarParent: {{sharedVarParent}}</div>
<child [(sharedVar)]="sharedVarParent"></child>
`,
directives: [ChildComponent]
})
export class ParentComponent {
sharedVarParent ='hello';
constructor() { console.clear(); }
}
我在 ParentComponent 中使用 sharedVarParent
只是为了证明 parent 和 child 中的名称不必相同。
如果你想在同一个组件中有多个双向数据绑定,你可以这样做
export class ComponentName {
@Input() public first: string = '';
@Input() public second: string = '';
@Output() public firstChange = new EventEmitter();
@Output() public secondChange = new EventEmitter();
public functionName1(first: string): void {
this.first = first;
this.firstChange.emit(first);
}
public functionName2(second: string): void {
this.second = second;
this.secondChange.emit(second);
}
}
当输入和输出被命名为 'x' 和 'xChange' 时,它会在属于同一父项的情况下自动检测它们。
我认为这实际上比接受的答案中的做法更好,因为它立即清楚了名称之间的关系。
我做了一个简单的 UI,它包含两个组件(父组件和子组件)。
UI 的作用是当我在子组件的输入框中键入一些内容时。该值将使用 ngModel 更改。
这样子组件就可以正常工作了。
// Child Component
@Component({
selector: 'child',
template: `
<p>{{sharedVar}}</p>
<input [(ngModel)]="sharedVar">
`
})
export class ChildComponent {
sharedVar: string;
}
现在我有一个父组件,我打算使用与子组件相同的值。
我将子组件添加到父模板中,并使用依赖注入调用子组件的sharedVar
。
// Parent Component
@Component({
selector: 'parent',
template: `
<h1>{{sharedVar}}</h1>
<child></child>
`,
directives: [ChildComponent],
providers: [ChildCompnent]
})
export class ParentComponent {
sharedVar: string;
constructor(child: ChildComponent) {
this.sharedVar = child.sharedVar;
}
}
问题是当我在输入框中输入时,<p>
中的值会自动更改,而父组件的 <h1>
中的值不会更改。
您可以设置从子级到父级的事件发射器通信 (outputs
)。例如像这样:
@Component({
selector: 'child',
template: `
<p>Child: {{sharedVar}}</p>
<input [(ngModel)]="sharedVar" (ngModelChange)="change()">
`
})
export class ChildComponent {
@Output() onChange = new EventEmitter();
sharedVar: string;
change() {
this.onChange.emit({value: this.sharedVar});
}
}
和父组件:
@Component({
selector: 'parent',
template: `
<h1>{{sharedVar}}</h1>
<child (onChange)="sharedVar = $event.value"></child>
`,
directives: [ChildComponent]
})
export class ParentComponent {
sharedVar: string;
constructor() {
}
}
我们可以在 parent 模板中使用 [(x)]
语法来实现 two-way 与 child 的数据绑定。如果我们创建一个名为 xChange
的输出 属性,Angular 将自动更新 parent 属性。每当 child 更改值时,我们确实需要 emit()
一个事件:
import {Component, EventEmitter, Input, Output} from 'angular2/core'
@Component({
selector: 'child',
template: `
<p>Child sharedVar: {{sharedVar}}</p>
<input [ngModel]="sharedVar" (ngModelChange)="change($event)">
`
})
export class ChildComponent {
@Input() sharedVar: string;
@Output() sharedVarChange = new EventEmitter();
change(newValue) {
console.log('newvalue', newValue)
this.sharedVar = newValue;
this.sharedVarChange.emit(newValue);
}
}
@Component({
selector: 'parent',
template: `
<div>Parent sharedVarParent: {{sharedVarParent}}</div>
<child [(sharedVar)]="sharedVarParent"></child>
`,
directives: [ChildComponent]
})
export class ParentComponent {
sharedVarParent ='hello';
constructor() { console.clear(); }
}
我在 ParentComponent 中使用 sharedVarParent
只是为了证明 parent 和 child 中的名称不必相同。
如果你想在同一个组件中有多个双向数据绑定,你可以这样做
export class ComponentName {
@Input() public first: string = '';
@Input() public second: string = '';
@Output() public firstChange = new EventEmitter();
@Output() public secondChange = new EventEmitter();
public functionName1(first: string): void {
this.first = first;
this.firstChange.emit(first);
}
public functionName2(second: string): void {
this.second = second;
this.secondChange.emit(second);
}
}
当输入和输出被命名为 'x' 和 'xChange' 时,它会在属于同一父项的情况下自动检测它们。
我认为这实际上比接受的答案中的做法更好,因为它立即清楚了名称之间的关系。