我知道Angular2没有双向数据绑定,但是否有办法模仿Angular1.x的双向数据绑定行为呢?
我知道Angular2没有双向数据绑定,但是否有办法模仿Angular1.x的双向数据绑定行为呢?
注意 - 向下滚动答案以获取ng-model绑定
你实际上可以做到这一点,只需要调用内部changlistener tick(类似于digest),以在区域中更新绑定,你可以添加一个(keyup)
事件来完成。同样,您也可以使用指令绑定,并使用组件设置的properties
字典。
例:
<input #label (keyup)>
<!-- variable #label represented as the element itself and accessible as property on controller instance
You can even bind keyup to a function or another another function and pass value from the label property-->
显示为:
<p>{{label.value}}</P>
父组件有一个文本框和一个标签。
import { Component, bootstrap} from '@angular/core';
import {Display} from 'display';
@Component({
selector: 'my-app',
template: `<p><b>Parent Component:</b><p><input #label (keyup) (change)="handleChange(label.value)">
<p>{{label.value}}</P> <display [text]="label"></display></p></p>`,
directives: [Display]
})
class MainComponent {
label: any;
constructor() {
}
handleChange(label){
this.label = label;
console.log(this.label);
}
}
现在也在子组件中显示:
@Component({
selector: 'edit',
template: `<p><b>Child Component:</b></p>{{text.value}}`
})
export class Edit {
@Input() text:any;
}
更新 - 用于双向绑定的ng-model
尽管Angular2默认是一次性绑定,但引入了ngModel
语法糖以实现双向绑定。例如:
<input ngControl="name" [(ngModel)]="name">
这里使用方括号([..]
)表示属性绑定,圆括号((..)
)表示事件绑定。基本上当您使用ng-model
时,您启用了绑定的两个方面,其中ngModel
更多的是一个事件。在幕后,它创建了一个可观察事件(EventEmitter
)来跟踪绑定元素中value
的更改,并相应地更新绑定的属性。
例如:
包含表单指令:
import {FORM_DIRECTIVES} from '@angular/common';
并且和表单一起使用
<form (ngSubmit)="onSubmit()" let-f="form">
<input ngControl="name" [(ngModel)]="name">
<button>Click me and check console</button>
</form>
没有表单
<input [(ngModel)]="name">
<button (click)="onSubmit()">Click me and check console</button>
不再必要
在视图注释中包含formDirectives依赖项。
@Component({
template: .....,
directives: [FORM_DIRECTIVES]
})
此外,阅读Victor Savkin的精彩文章,了解在angular2中通过创建ng-model事件以及其工作原理来实现双向绑定。
FormsModule
。 - zed在Angular2中确实有双向绑定。请参见此处:https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ngModel
那么,在自定义组件中如何使用它呢?
我喜欢做的事情是这样的:
private currentSelectedItem: MachineItem;
@Output() selectedItemChange: EventEmitter<MachineItem> = new EventEmitter<MachineItem>();
@Input() set selectedItem(machineItem: MachineItem) {
this.currentSelectedItem = machineItem;
this.selectedItemChange.emit(machineItem);
}
get selectedItem(): MachineItem {
return this.currentSelectedItem;
}
然后像这样使用它
<admin-item-list [(selectedItem)]="selectedItem"></admin-item-list>
你也可以在值被更改时直接发出新值。但我觉得在设置器方法中全局完成这个操作非常方便,这样在将其直接绑定到视图时就不必再费心了。
http://plnkr.co/edit/lOFzuWtUMq1hCnrm9tGA?p=preview
创建一个组件,它具有内部属性来保存标签this.label
和回调changeLabel
,该回调期望一个事件对象。@Component({
selector: 'app',
templateUrl: 'bound.html'
})
class App {
label: string;
constructor() {
this.label = 'default label'
}
changeLabel(event) {
this.label = event.target.value;
}
}
bootstrap(App);
<label for="myinput">{{label}}</label>
<input id="myinput" type="text"/>
<p></p>You can change the label above by typing something below</p>
<label for="labeltext">New Label Text</label>
<input type="text" id="labeltext" (change)="changeLabel($event)"/>
有另一种方法可以欺骗Angular2实现双向绑定。不要传递属性,而是将对象传递给组件。如果通过单向绑定传递对象,则其所有属性实际上都是双向绑定的。这使得组件变得不够灵活,因为它需要知道对象,但在许多情况下仍然很有用。
我有一个看起来像这样的组件:
import { Component, Input } from "@angular/core";
import { NgSwitch, NgSwitchWhen, NgSwitchDefault } from "@angular/common";
export class Movie
{
public Title: string;
public Rating: number;
public Seen: boolean;
}
@Component
({
selector: "hh-image-checkbox",
template: `
<div [ngSwitch]="movie.Seen">
<div *ngSwitchWhen="true">
<img src="/Content/res/CheckTrue.png" (click)="onClick()">
</div>
<div *ngSwitchDefault>
<img src="/Content/res/CheckFalse.png" (click)="onClick()">
</div>
</div>
`,
directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]
})
export class ImageCheckboxComponent
{
@Input() movie: Movie;
public onClick()
{
this.movie.Seen = !this.movie.Seen;
}
}
<hh-image-checkbox [movie]="movie"></hh-image-checkbox>
NgSwitch,NgSwitchWhen,NgSwitchDefault
并将它们添加到directives
中。它们由PLATFORM_DIRECTIVES
全局提供。 - Günter Zöchbauer<input [(ngModel)]="name" />
<input [value]="name" />
<input (input)="name=$event.target.value">
来自文档:
Two-way binding (
[(...)]
)You often want to both display a data property and update that property when the user makes changes.
On the element side that takes a combination of setting a specific element property and listening for an element change event.
Angular offers a special two-way data binding syntax for this purpose,
[(x)]
. The[(x)]
syntax combines the brackets of property binding,[x]
, with the parentheses of event binding,(x)
.[( )] = BANANA IN A BOX
Visualize a banana in a box to remember that the parentheses go inside the brackets.
更多信息请参见
很简单,试试这个:
<input [(ngModel)]="property" placeholder="property Value"/>
<h1>{{property}}</h1>