ng-metadata @Directive:如何设置输入值?

本杰明·M

我有一个非常简单的指令,它应该更新模糊的输入值。

模糊处理程序被调用,但我无法设置输入值。

import {NgModel} from 'ng-metadata/common';
import {Directive, Inject, Self, HostListener} from 'ng-metadata/core';

@Directive({
  selector: 'foo'
})
export class FooDirective {
  constructor(
    private @Self() ngModel: NgModel,
    private @Inject('$element') $element: ng.IAugmentedJQuery
  ) {}

  @HostListener('blur')
  onBlur() {
    this.ngModel.$viewValue = "FOO";
    this.ngModel.$modelValue = "FOO";
    this.ngModel.$setViewValue("FOO");
    this.ngModel.$commitViewValue();
    // I guess, I just need one of these, but none did work
  }
}

HTML代码:

<input type="text" foo ng-model="$ctrl.abc"></input>

我可以使用 读取当前值console.log(this.ngModel.$modelValue),但我无法设置新值。


我错过了什么?

本杰明·M

知道了!

这很简单:

this.ngModel.$setViewValue("FOO");
this.ngModel.$render();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章