2017-09-07 57 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'); }); 

希望这会有所帮助。