Advertisement
Guest User

Untitled

a guest
May 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { DecimalPipe } from '@angular/common';
  3. import { ToNumberPipe } from './to-number.pipe';
  4.  
  5. @Component({
  6. selector: 'my-app',
  7. template: `<input type='text' [(ngModel)]="model">
  8. <div style="margin-top:1em;"> model數值: {{ _model || 0}}</div>
  9. <div style="margin-top:1em;"> model格式化數值: {{ model || 0}}</div>`,
  10. providers: [
  11. ToNumberPipe,
  12. DecimalPipe
  13. ]
  14. })
  15. export class AppComponent {
  16. _model;
  17. get model() {
  18. return this._model? this.decimalPipe.transform(this._model,'2.'): 0;
  19. };
  20. set model(val) {
  21. this._model = val ? this.toNumberPipe.transform(val): 0;
  22. };
  23. constructor(
  24. private toNumberPipe: ToNumberPipe,
  25. private decimalPipe: DecimalPipe,
  26. ) {}
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement