Ron*_*erg 21 data-binding 2-way-object-databinding angular
我知道Angular2没有双向数据绑定,但有没有办法模仿Angular1.x的双向数据绑定行为?
PSL*_*PSL 19
注意 - 向下滚动ng-model绑定的答案
您实际上可以这样做,只需要调用内部changelistener tick(类似于摘要)来更新区域中的绑定,您只需添加一个(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-->
Run Code Online (Sandbox Code Playgroud)
展示为:
<p>{{label.value}}</P>
Run Code Online (Sandbox Code Playgroud)
父组件具有文本框和标签.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在也在子组件中显示它:
@Component({
selector: 'edit',
template: `<p><b>Child Component:</b></p>{{text.value}}`
})
export class Edit {
@Input() text:any;
}
Run Code Online (Sandbox Code Playgroud)
更新 - 用于双向绑定的ng-model
虽然Angular2默认是一次性绑定,ngModel但引入了糖以实现双向绑定.你可以做到这一点:
<input ngControl="name" [(ngModel)]="name">
Run Code Online (Sandbox Code Playgroud)
这里使用方括号([..])表示属性绑定和圆括号((..))用于事件绑定.基本上,当您使用时ng-model,您启用两个绑定ngModel更多的是一个事件.在幕后,它创建一个可观察事件(with EventEmitter)来跟踪value绑定元素中的更改并分别更新绑定属性.例如:-
包括formDirectives:
import {FORM_DIRECTIVES} from '@angular/common';
Run Code Online (Sandbox Code Playgroud)
和形式
<form (ngSubmit)="onSubmit()" let-f="form">
<input ngControl="name" [(ngModel)]="name">
<button>Click me and check console</button>
</form>
Run Code Online (Sandbox Code Playgroud)
没有形式
<input [(ngModel)]="name">
<button (click)="onSubmit()">Click me and check console</button>
Run Code Online (Sandbox Code Playgroud)
不再需要
在视图注释中包含formDirectives依赖项.
@Component({
template: .....,
directives: [FORM_DIRECTIVES]
})
Run Code Online (Sandbox Code Playgroud)
还可以通过创建ng-model事件及其工作原理,阅读Victor Savkin关于angular2中双向绑定的精彩内容.
JDT*_*LH9 15
您现在可以使用以下语法通过使用ngModel来执行此操作:
<input [(ngModel)]="myProp" />
Run Code Online (Sandbox Code Playgroud)
方括号和圆括号的组合表示"双向绑定".
请看这里的插件
Mar*_*Eis 11
是的,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;
}
Run Code Online (Sandbox Code Playgroud)
并使用它
<admin-item-list [(selectedItem)]="selectedItem"></admin-item-list>
Run Code Online (Sandbox Code Playgroud)
您还可以在实际更改的位置发出新值.但是我觉得在setter方法中这样做非常方便,并且不必费心,例如当我将它直接绑定到我的视图时.
您可以通过附加到输入字段上的事件并更新内部值来执行此操作,如下例所示:
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);
Run Code Online (Sandbox Code Playgroud)
创建模板并将回调附加到相应的事件(您可以将其附加到keypress事件,但是您可能需要超时.change为了简单起见,我将它附加到事件中(这意味着您可能需要关闭输入以查看更新).
<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)"/>
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以将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;
}
}
Run Code Online (Sandbox Code Playgroud)
它被调用如下:
<hh-image-checkbox [movie]="movie"></hh-image-checkbox>
Run Code Online (Sandbox Code Playgroud)
电影对象本身是单向绑定的,但其所有属性都可用于双向绑定.
| 归档时间: |
|
| 查看次数: |
57178 次 |
| 最近记录: |