简体   繁体   English

打字稿成员变量类类型更改

[英]typescript member variable class type changing

I'm wrapping jquery's datepicker for use in angular2 and am running into a situation where the class of a member variable is changing somehow. 我包装了jquery的datepicker以在angular2中使用,并且遇到了成员变量的类以某种方式更改的情况。 For reference, I'm a beginner here. 供参考,我是这里的初学者。

First, here is the code (based on the example from http://www.radzen.com/blog/jquery-plugins-and-angular/ ): 首先,下面是代码(基于http://www.radzen.com/blog/jquery-plugins-and-angular/的示例):

import { forwardRef, ViewChild, Input, Output, EventEmitter, ElementRef, AfterViewInit, OnDestroy, Component} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

import * as $ from "jquery";
import 'jqueryui';

const DATE_PICKER_VALUE_ACCESSOR = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => DatePickerComponent),
    multi: true
};

@Component({
    selector: 'qnet-datepicker',
    template: `<input #input type="text">`,
    providers: [DATE_PICKER_VALUE_ACCESSOR]
})
export class DatePickerComponent implements AfterViewInit, ControlValueAccessor, OnDestroy {
    private onTouched = () => {};
    private onChange: (date: Date) => void = () => {};

    @Input() date: Date;
    @Input() options: any = {};
    @Output() dateChange = new EventEmitter();

    @ViewChild('input') input: ElementRef;

    constructor() {
        this.date = new Date();
    }

    writeValue(date: Date) {
        if(!date) {
            return;
        }
        this.date = date;
        $(this.input.nativeElement).datepicker('setDate', this.date)
    }

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

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

    ngAfterViewInit() {
        $(this.input.nativeElement).datepicker(Object.assign({}, this.options, {
            onSelect: (dateStr: string) => {
//              this.date = $(this.input.nativeElement).datepicker('getDate');
//              this.onChange(this.date);
                this.onTouched();
                this.dateChange.next(this.date);
            }
        }))
console.log('date is ' + this.date)
        $(this.input.nativeElement).datepicker('setDate', this.date)
    }

    ngOnDestroy() {
        $(this.input.nativeElement).datepicker('destroy');
    }
}

In the constructor the type of this.date is 'Date' but by time I get to ngAfterViewInit the type has somehow changed to 'DatePickerComponent'. 在构造函数中,this.date的类型为“ Date”,但到了ngAfterViewInit时,类型已更改为“ DatePickerComponent”。 How can this happen? 怎么会这样

edit 编辑

Additional info: the html using the datepicker was something like 附加信息:使用datepicker的html类似于

<qnet-datepicker #startDate [date]="startDate" (dateChange)="updateStart(startDate.date, startTime.value)"></qnet-datepicker>

As ahmed describes in his answer, this ended up accidentally binding the member variable to the element instead of the date. 正如艾哈迈德(ahmed)在回答中描述的那样,这最终意外地将成员变量绑定到元素而不是日期。 Part of my confusion was relying on typescript being strongly typed, but hookups between html and logic is done via javascript so no longer has the strongly type'd property. 我的部分困惑是依赖于强类型的打字稿,但是html和逻辑之间的连接是通过javascript完成的,因此不再具有“强类型”的属性。

After a look at the github repo , the issue is this: 在查看github repo之后 ,问题是这样的:

daterangepicker.component.ts line 12 <qnet-datepicker #startDate [date]="startDate" (dateChange)="updateStart(startDate.date, startTime.value)"></qnet-datepicker> daterangepicker.component.ts第12行<qnet-datepicker #startDate [date]="startDate" (dateChange)="updateStart(startDate.date, startTime.value)"></qnet-datepicker>

the input [date] is taking a reference to the component itself via template reference variable #startDate input [date]通过模板引用变量#startDate引用了组件本身

removing that fixes the issue. 删除即可解决问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM