简体   繁体   English

实施ControlValueAccessor的Angular 2指令不会在更改时更新“已触摸”属性

[英]Angular 2 Directive implementing ControlValueAccessor doesn't update 'touched' property on change

I'm trying to create my own Angular 2 Directive for Jquery UI Datepicker. 我正在尝试为Jquery UI Datepicker创建自己的Angular 2指令。 I've seen some different approaches on the internet and in SO as well, but no one achieves the goal that I want to. 我在互联网上看到了一些不同的方法,但也没有人达到我想要的目标。 So this is the code that I have so far: 所以这是我到目前为止的代码:

import {Directive, ElementRef, Input, forwardRef} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";

declare  var $:any;

@Directive({
  selector: '[date-picker]',
  providers: [{
    provide: NG_VALUE_ACCESSOR,useExisting:
      forwardRef(() => DatePickerDirective),
    multi: true
  }]
})
export class DatePickerDirective implements ControlValueAccessor {
  private value: string;

  @Input('changeMonth') changeMonth:boolean = true;
  @Input('changeYear') changeYear:boolean = true;

  constructor(private el: ElementRef) {

  }

  ngAfterViewInit(){
    $(this.el.nativeElement).datepicker({
      changeMonth: this.changeMonth,
      yearRange: "1:100",
      changeYear: this.changeYear
    }).on('change', e => this.onChange(e.target.value));
  }

  onChange: Function = () => {};

  onTouched: Function = () => {};

  writeValue(val: string) : void {
    this.value = val;
  }

  registerOnChange(fn: Function): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: Function): void {
    this.onTouched = fn;
  }
}

What is happening is that even when I select a date (picker) or type it directly on input field, it isn't updating "touched" property. 发生的事情是,即使我选择日期(选择器)或直接在输入字段上键入它,它也不会更新“触摸”属性。

Do you have any ideas for fixing it? 你对修复它有什么想法吗?

For those who eventually have the same problem, I figured out a way to manage it, as you can below: 对于那些最终遇到同样问题的人,我想出了一种方法来管理它,如下所示:

import {Directive, ElementRef, Input, forwardRef} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms";

declare  var $:any;

export const CUSTOM_INPUT_DATE_PICKER_CONTROL_VALUE_ACCESSOR: any = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => DatePickerDirective),
  multi: true
};

@Directive({
  selector: '[date-picker]',
  host: {'(blur)': 'onTouched($event)'},
  providers: [CUSTOM_INPUT_DATE_PICKER_CONTROL_VALUE_ACCESSOR]
})
export class DatePickerDirective implements ControlValueAccessor {
  private innerValue: string;

  @Input('changeMonth') changeMonth:boolean = true;
  @Input('changeYear') changeYear:boolean = true;

  constructor(private el: ElementRef) {
    $(this.el.nativeElement).datepicker({
      changeMonth: true,
      changeYear: true,
      dateFormat: 'dd/mm/yy'
    }).on('change', e => this.onChange(e.target.value));
  }

  public onChange: any = (_) => { /*Empty*/ }
  public onTouched: any = () => { /*Empty*/ }

  get value(): any {
    return this.innerValue;
  };

  //set accessor including call the onchange callback
  set value(v: any) {
    if (v !== this.innerValue) {
      this.innerValue = v;
      this.onChange(v);
    }
  }

  writeValue(val: string) : void {
    this.innerValue = val;
  }

  registerOnChange(fn: any): void {
    this.onChange = fn;
  }

  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 组件属性不会在引用属性更改时更新 - Angular component property doesn't update upon change in referencing property 如何使用 ControlValueAccessor 将 Angular 中的自定义输入标记为已触摸? - How to mark as touched a custom input in Angular with ControlValueAccessor? 自定义ControlValueAccessor似乎不适用于我自己的FormControl指令 - Custom ControlValueAccessor doesn't seem to work for my own FormControl directive ControlValueAccessor并感动 - ControlValueAccessor and touched Angular 7 Custom mat-checkbox不会使用ControlValueAccessor更新表单有效性 - Angular 7 Custom mat-checkbox doesn't update form validity with ControlValueAccessor 如果属性未以角度4更改,如何强制更新模板 - How to force an update to a template when a property doesn't change in angular 4 Angular 2自定义指令不更新模型 - Angular 2 custom directive doesn't update the model Angular 材料垫按钮不会更新外部指令的禁用更改 - Angular material mat-button doesn't update the disabled change from outer directive 为什么 ControlValueAccessor 不更新我在组件内的值? - Why ControlValueAccessor doesn't update my value inside the component? 如何使用 ControlValueAccessor Angular 在自定义输入中使用指令 - How To Use A Directive In A Custom Input With ControlValueAccessor Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM