简体   繁体   English

用于 2 种属性绑定的 Angular2 装饰器

[英]Angular2 decorator for 2 way property binding

From Victor Savkin's post on Angular2 template syntax , shows how to use input and output property binding -来自 Victor Savkin 关于Angular2 模板语法的帖子,展示了如何使用输入和输出属性绑定 -

<todo-cmp [model]="todo" (complete)="onCompletingTodo(todo)"></todo-cmp>

@Component({selector: 'todo-cmp'})
class TodoCmp {
  @Input() model;
  @Output() complete = new EventEmitter(); // TypeScript supports initializing fields
}

The input property is decorated with @Input() while output property has @Output().输入属性用@Input() 修饰,而输出属性用@Output() 修饰。 How should I declare a property which is going to have a 2 way property binding?我应该如何声明将具有 2 种属性绑定的属性? Example: Assuming rootpanel component has 'suggestions' property (of type string) and searchPanel has 'getSuggestions property.示例:假设 rootpanel 组件具有“suggestions”属性(字符串类型)并且 searchPanel 具有“getSuggestions”属性。 Now I want the two properties to be bound to each other both ways.现在我希望这两个属性以两种方式相互绑定。 I tried -我试过 -

rootpanel.html:根面板.html:

<search-panel [(getSuggestions)]="suggestions"> </search-panel>

But I am stuck while declaring the getSuggestions property in the searchPanel component.但是我在 searchPanel 组件中声明 getSuggestions 属性时卡住了。 Also what should be the type of the getSuggestions property - string or EventEmitter<string> ?另外 getSuggestions 属性的类型应该是什么 - string or EventEmitter<string>

Please suggest.请建议。

If you want two-way model binding from the parent component:如果你想从父组件进行双向模型绑定:

[(model)]

You need the following in your child component:您的子组件中需要以下内容:

@Input() model: string;
@Output() modelChange:EventEmitter<string>;

At some point when the model is overwritten in your child component, you'll emit the modelChange event:在某些时候,当模型在您的子组件中被覆盖时,您将发出modelChange事件:

updateModel(newValue:string) {
    this.model = newValue;
    this.modelChange.emit(this.model);
}

From the parent component's perspective, [(model)] is equivalent to:从父组件的角度来看, [(model)]等同于:

[model]="model"  (modelChange)="model=$event"

In this way, when the model property changes inside a child component, the change in the model propagates upwards though two-way binding, synching all the bound models along the way.这样,当子组件内部的模型属性发生变化时,模型的变化会通过双向绑定向上传播,沿途同步所有绑定的模型。

If you want to use [(getSuggestions)] -style for two-way-binding declare the fields like如果您想使用[(getSuggestions)]样式进行双向绑定,请声明如下字段

class TodoCmp {
  @Input() getSuggestions;
  @Output() getSuggestionsChange = new EventEmitter(); 

  onClick() {
    getSuggestions = 'someValue';
    getSuggestionsChange.emit(getSuggestions);
  }
}

getSuggestions is probably not a good choice for such a input/output combination but it should demonstrate how they are connected.对于这样的输入/输出组合, getSuggestions可能不是一个好的选择,但它应该展示它们是如何连接的。 The output needs to have the same name as the input with an additional Change .输出需要与输入具有相同的名称,并带有额外的Change If this naming scheme doesn't fit use your component like如果此命名方案不适合使用您的组件,例如

<search-panel [suggestions]="suggestions" (getSuggestions)="updateSuggestions($event)> </search-panel>

with input/output like输入/输出像

class TodoCmp {
  @Input() suggestions;
  @Output() getSuggestions = new EventEmitter(); 

  onClick() {
    suggestions = 'someValue';
    getSuggestions.emit(getSuggestions);
  }
}

The approach pixelbits recommended is exactly how you should do that, but if you have multiple two-way data binding properties on a component, or even one that changes frequently in your codebase, I created a decorator for that. pixelbits 推荐的方法正是你应该如何做到这一点,但如果你在一个组件上有多个双向数据绑定属性,或者甚至在你的代码库中经常更改的属性,我为此创建了一个装饰器。 If you are using npm here it is.如果你在这里使用 npm,它就是。 Just go to the gihub page if you need the code.如果您需要代码,请转到 gihub 页面。

With this, you can directly use:有了这个,你可以直接使用:

 import { Component } from '@angular/core'; import { TwoWay } from 'two-way-decorator'; @Component({ selector: 'app-example', templateUrl: './example.component.html', styleUrls: ['./example.component.scss'] }) export class ExampleComponent { @TwoWay() public text: string; @TwoWay() public count: number; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM