在 jquery 插件事件上更新 [(ngModel)]

Update [(ngModel)] on jquery plugin event

有什么方法可以在 jquery 事件上刷​​新 ngModel,例如

  1. 'dp.change' 事件上的日期时间选择器。
  2. 在 select 更改事件上选择 2。

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

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

希望对您有所帮助。

我们可以做类似的事情。它对我有用。

在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());
    });
  }
}