Search code examples
angularrxjsangular2-changedetection

Angular 2 equivalent of Ember.Computed('Prop1','Prop2'...'prop n')


I am trying to port my Ember Application to Angular 2 , but i failed to see how can I create
Computed Properties--Properties observing other properties for changes and the reacting
in Angular 2.

[(myVar)] &&  onMyVarChange= new EventMitter();

Observes changes to it self and the react.

Any help/directions will be great.

Update :

Solved it using answer from @Nazim Used typescript properties

TS (compoment.ts)

  private _isValid: boolean;
  public get isValid(): boolean {
    return this._isValid;
  }
  public set isValid(v: boolean) {
    this._isValid = v;
  }
  // A read only property
  private _show: boolean;
  public get show(): boolean {
    return this._isValid;
  }  

Template (component.html)

 <h2 *ngIf="show">Show Me</h2>


Solution

  • From what I understand, Angular 2 uses native ES2015 computed properties. For ex. you could define a Person Component:

    export class PersonComponent {
      firstName: string;
      lastName: string;
    
      get fullName() {
        return `${this.firstName} ${this.lastName}`;
     }
    } 
    

    And then use ngModel to bind the value of fullName to a template element.