简体   繁体   English

Angular 2:如何使用getter和setter方法将Input属性绑定到model属性?

[英]Angular 2: How to bind Input to model property with getter and setter methods?

I'm making an Angular 2 web app. 我正在制作一个Angular 2网络应用程序。 I have a model that is comprised of a few key properties, and then several other properties that are computed based off those key values. 我有一个由几个关键属性组成的模型,然后是基于这些关键值计算的其他几个属性。

All of my properties have getter methods. 我的所有属性都有getter方法。 To keep my computed properties in sync with my key properties, the key properties are changed via setter methods which re-evaluate all of the computed properties. 为了使我的计算属性与我的键属性保持同步,通过setter方法更改键属性,这些方法重新计算所有计算属性。 Here's a simplified example: 这是一个简化的例子:

export class Model{
    private _keyValue: number;
    private _computedValue: number;

    getKeyValue(): number{
        return this._keyValue;
    }

    setKeyValue(value: number){
        this._keyValue = value;
        this.evaluateComputedValues(); // This might be time-consuming
    }

    getComputedValue(): number{
        return this._computedValue;
    }
}

This keeps my model consistent: every time one of the key properties is changed, all of the computed properties are re-computed. 这使我的模型保持一致:每次更改其中一个关键属性时,都会重新计算所有计算属性。

Now I need to figure out how to bind my properties to my component views. 现在我需要弄清楚如何将我的属性绑定到组件视图。 It seems like I can present the computed property getters using interpolation: 看起来我可以使用插值来呈现计算属性的getters:

<div>{{model.getComputedValue()}}</div>

However, I'm not sure what the best way to bind my key properties to input fields would be. 但是,我不确定将我的键属性绑定到输入字段的最佳方法是什么。 All of the examples of using two-way binding seem to use ngModel like this: 使用双向绑定的所有示例似乎都使用这样的ngModel:

<input [(ngModel)]='model.property'>

However, that seems geared towards binding to simple properties. 但是,这似乎是为了绑定到简单的属性。 I ideally need two-way binding using my separate getter and setter methods (getKeyValue and setKeyValue). 理想情况下,我需要使用单独的getter和setter方法(getKeyValue和setKeyValue)进行双向绑定。

Is there any built-in way to accomplish this in Angular 2? 在Angular 2中有没有内置的方法来实现这一点?

You need to use this longer form 你需要使用这个更长的形式

<input [ngModel]='model.getProperty()' (ngModelChange)="model.setProperty($event)">

You should be aware that the getXxx() methods will be called every time change detection runs, which can be quite often. 您应该知道,每次更改检测运行时都会调用getXxx()方法,这可能经常发生。 Ensure that the getters return the same value (especially for objects the same instance) and ensure the getters don't have side effects, otherwise you'll get "Expression has changed since it was last checked ..." errors. 确保getter返回相同的值(特别是对于同一实例的对象),并确保getter没有副作用,否则你会得到“表达式自上次检查后已经更改...”错误。

not sure what version of Angular is being used above, but the version I'm using (v4.3.5) allows binding directly to the getter/setter methods of a field using ngModel: in the typescript file: 不确定上面使用的是什么版本的Angular,但我正在使用的版本(v4.3.5)允许使用ngModel:在typescript文件中直接绑定到字段的getter / setter方法:

  get AnnualRevenueFormatted():string{
    return  this.AnnualRevenue !== undefined ?  `$${this.AnnualRevenue.toLocaleString()}`: '$0';
  }

  // strip out all the currency information and apply to Annual Revenue
  set AnnualRevenueFormatted(val: string) {
    const revenueVal = val.replace(/[^\d]/g, '');
    this.AnnualRevenue = parseInt(revenueVal);
  }

and in the template file 并在模板文件中

<input type="text" class="text-input" [(ngModel)]="accountInfo.AnnualRevenueFormatted"  />

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

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