Types Angular 2 Modelbinding到simpletype(字符串)不起作用
我有一个简单的组件,其中有一个字符串字段的输入:Types Angular 2 Modelbinding到simpletype(字符串)不起作用,types,typescript,angular,model-binding,Types,Typescript,Angular,Model Binding,我有一个简单的组件,其中有一个字符串字段的输入: import {Component, Input} from 'angular2/core'; @Component({ selector: 'mundo-input', template: ` <input class="form-control" [(ngModel)]="zeit" /> ` }) export class MundoInputComponent { @In
import {Component, Input} from 'angular2/core';
@Component({
selector: 'mundo-input',
template: `
<input class="form-control" [(ngModel)]="zeit" />
`
})
export class MundoInputComponent {
@Input() zeit: string;
}
从'angular2/core'导入{组件,输入};
@组成部分({
选择器:“mundo输入”,
模板:`
`
})
导出类MundoInputComponent{
@Input()zeit:string;
}
我正在使用此组件,如下所示:
<mundo-input [(zeit)]="myzeit"></mundo-input>
外部组件的myzeit属性被正确注入。当我手动更改值并在外部组件上按save时,myzeit属性具有旧值
我将zeit的类型从string更改为Hero类(如NG2教程中所述),并将输入的绑定更改为zeit.name。双向数据绑定有效
是否可以从外部组件绑定到string类型的属性?或者,对于复杂类型(类)是否可能?您需要向组件添加一个输出,以便能够利用双向绑定:
@Component({
selector: 'mundo-input',
template: `
<input class="form-control" [ngModel]="zeit"
(ngModelChange)="onChange($event)" />
`
})
export class MundoInputComponent {
@Input() zeit: string;
@Output() zeitChange:EventEmitter<string> = new EventEmitter();
onChange(val) {
this.zeitChange.emit(val);
}
}
在您的情况下,只对zeit
参数使用单向绑定
看看这个问题:。有什么不同吗?我先编辑了你链接的问题,但又退了回来,因为这是一个有意义的不同问题,但仍然不起作用。为什么我需要一个简单类型的输出,但是类在没有输出的情况下工作?对于primitive类型,你不能通过引用在子组件中进行更新…真棒的人,我忘记了将zeit放在括号中,就像非常感谢!
<mundo-input [(zeit)]="myzeit"></mundo-input>