2017-09-07 84 views

回答

0

我們可以這樣做。這個對我有用。

在HTML模板

<input type="text" class="form-control" [listItem]="list" autocomplete id="txtbox" name="txtbox" placeholder="myplaceholder" [(ngModel)]="myModel"> 

在打字稿代碼

import { Directive, ElementRef, Input } from '@angular/core'; 
import { NgModel } from '@angular/forms'; 
declare var $; 
@Directive({ 
    selector: '[ngModel][autocomplete]', 
    providers: [NgModel] 
}) 
export class Autocomplete { 
    @Input() listItem: any = []; 
    constructor(public _elementRef: ElementRef, private ngModel: NgModel) { 
    } 
    ngAfterViewInit() { 
    let that = this; 
    $(this._elementRef.nativeElement).typeahead({ 
     source: this.listItem.map(item => { 
     return { id: item.account.toString(), name: item.account.toString() }; 
     }) 
    }); 

    this.ngModel.valueChanges.subscribe(function(value) { 
     /* Set any value of your custom control */ 
     $(this._elementRef.nativeElement).data("newValue", value); 
    }); 

    /* Inform ng model for any new change happened */ 
    $(this._elementRef.nativeElement).bind("change",()=>{ 
     that.ngModel.update.emit($(that._elementRef.nativeElement).val()); 
    }); 
    } 
} 
0

我使用相同的插件,這就是我刷新模型的方式。

angular.element('#datetimepicker_id').on('dp.change', function(e){ $scope.date_model = angular.element('#datetimepicker_id').data('date'); }); 

希望這會有所幫助。