没有点击Angular 2字段的格式编号?

时间:2017-12-04 04:54:48

标签: angular typescript

我找到了一个使用管道格式化输入中数字的指令。例如1000 - 1,000。但为此你需要点击该字段。我无法绑定转换函数来加载页面。我试图这样做:

@HostListener('load', ['$event']) onPageLoad(event: Event) 
   onBlur(value) {
        this.el.value = this.currencyPipe.transform(value);
    }

这是指令的代码,我没做错?

import { Directive, HostListener, ElementRef, OnInit } from "@angular/core";
import { MyCurrencyPipe } from "./my-currency.pipe";

@Directive({ selector: "[myCurrencyFormatter]" })
export class MyCurrencyFormatterDirective implements OnInit {

    private el: any;

    constructor(
        private elementRef: ElementRef,
        private currencyPipe: MyCurrencyPipe
    ) {
        this.el = this.elementRef.nativeElement;

    }

    ngOnInit() {
        this.el.value = this.currencyPipe.transform(this.el.value);
    }

    @HostListener("focus", ["$event.target.value"])
    onFocus(value) {
        this.el.value = this.currencyPipe.parse(value); // opossite of transform
    }

    @HostListener("blur", ["$event.target.value"])
    onBlur(value) {
        this.el.value = this.currencyPipe.transform(value);
    }

    @HostListener('load', ["$event.target.value"])
    onload(value) {
        this.el.value = this.currencyPipe.transform(value);
    }

}

0 个答案:

没有答案