Angular2 - 指令不更新模型

标签 angular angular2-directives

我在一个包含数值的 html 输入框上有一个指令。当用户将数字粘贴到文本框中时,我有一个“清除”数字的指令(去除逗号、美元符号等)。清理代码似乎工作正常,但是即使文本框显示我的清理值,我的模型也没有更新清理值。

如何使用新值更新我的模型?

Plnkr here

这是一个精简的例子:

app.ts

@Component({
  selector : 'my-app',
  template : `
  <div>
    <br/>
    <br/>
    <p>Stack Overflow person - give focus to text box and then lose focus by clicking elsewhere in the screen. <br/>The model is not updated.</p>
    <br/>Model value: {{ balanceAmount }}
    <br/>
    <br/>
    <input type="text" [(ngModel)]="balanceAmount" myCurrencyFormatter  /><br/>
  </div>
  `,
})
export class App {
  name:string;
  constructor(private mycurpipe: MyCurrencyPipe) {
    this.balanceAmount = 1234567.89;
  }
}

Currency-Formatter-Directive.ts

@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.cleanNumber(value); //"987654" ;//this.currencyPipe.transform(value);
  }
  
  cleanNumber (value: number) {
    return 8888888; // clean logic goes here, removed for plunk example
  }
  

}

Plnkr here

最佳答案

您需要为您的模型添加Emitter。这是在 Angular 2 中实现双向绑定(bind)的方法。看看行 @Output() ngModelChange = new EventEmitter(); 以及我如何使用此变量将更改发送给调用者。

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

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

  private el: any;
  @Output() ngModelChange = new EventEmitter();

  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); // oposite of transform
    this.ngModelChange.emit(this.el.value);
  }

  @HostListener("blur", ["$event.target.value"])
  onBlur(value) {
    this.el.value = this.cleanNumber(value); //"987654" ;//this.currencyPipe.transform(value);
    this.ngModelChange.emit(this.el.value);
  }
  
  cleanNumber (value: number) {
    return 8888888;
  }

}

关于Angular2 - 指令不更新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565986/

相关文章:

angular - 运行 jasmine 测试用例后出现错误 :- TypeError: this. snackBarRef.dismiss 不是一个函数

Angular2 事件发射器未得到处理

javascript - 在 Angular 2 中使用普通 js 代码 (angular-cli)

javascript - Angular 2 : Error during evaluation of click

javascript - Angular2+ : how does NgModel/NgControl internally handle updates from view to model?

angular - 如何从angular2中的子组件更新父组件

javascript - Angular 路由器看不到要重定向的组件

javascript - 如何在 Angular 2 循环中每行显示两个表列(Angular2 相当于 PHP 的 array_chunk)

.net - oauth 中不支持的响应类型

angular - 无法从父结构指令访问子结构指令