Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Propiedad del componente de sobrecarga angular con getter y setter con decorador

¿Cómo anular un getter y un setter en el objeto y agregar algo de lógica pero continuar llamando a los métodos originales?

 import { Component, OnInit } from '@angular/core'; function defineDecorator() { return function (target: object, propertyKey: string) { console.log('decorate', target, propertyKey); const descriptor = Object.getOwnPropertyDescriptor(target, propertyKey); const originalSet = descriptor && descriptor.set ? descriptor.set : null; const originalGet = descriptor && descriptor.get ? descriptor.get : null; Object.defineProperty(target, propertyKey, { get: function (): any { console.log('overload getter called'); if (originalGet) { originalGet.call(this); } return 'test'; }, set: function (value: any) { console.log('overload setter called'); if (originalSet) { originalSet.call(this, value); } }, enumerable: true, configurable: true, }); }; } @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent implements OnInit { _foo: string; @defineDecorator() set foo(value: string) { console.log('original setter called'); this._foo = value; } get foo() { console.log('original getter called'); return this._foo; } ngOnInit(): void { this.foo = 'bar'; } }

demostración en línea https://stackblitz.com/edit/angular-ivy-qj1p7a?file=src%2Fapp%2Fapp.component.ts

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!