@@ -2,12 +2,12 @@ import {
22 Directive , Input , Output , HostListener , EventEmitter , OnInit , ElementRef ,
33 Renderer , DynamicComponentLoader , ComponentRef , ReflectiveInjector , provide , ViewContainerRef
44} from '@angular/core' ;
5- import { NgModel } from '@angular/forms' ;
5+ import { NgControl , FormControl } from '@angular/forms' ;
66import { TypeaheadUtils } from './typeahead-utils' ;
77import { TypeaheadContainerComponent } from './typeahead-container.component' ;
88import { TypeaheadOptions } from './typeahead-options.class' ;
99
10- import { Observable } from 'rxjs/Rx ' ;
10+ import { Observable } from 'rxjs/Observable ' ;
1111
1212import 'rxjs/add/observable/from' ;
1313import 'rxjs/add/operator/debounceTime' ;
@@ -21,13 +21,10 @@ import {global} from '@angular/core/src/facade/lang';
2121const KeyboardEvent = ( global as any ) . KeyboardEvent as KeyboardEvent ;
2222/* tslint:enable */
2323
24- // https://github.com/angular/angular/blob/master/modules/@angular /src/core/forms/directives/shared.ts
25- function setProperty ( renderer :Renderer , elementRef :ElementRef , propName :string , propValue :any ) :void {
26- renderer . setElementProperty ( elementRef . nativeElement , propName , propValue ) ;
27- }
28-
2924@Directive ( {
30- selector : '[typeahead][ngModel]'
25+ /* tslint:disable */
26+ selector : '[typeahead][ngModel],[typeahead][formControlName]'
27+ /* tslint:enable */
3128} )
3229export class TypeaheadDirective implements OnInit {
3330 @Output ( ) public typeaheadLoading :EventEmitter < boolean > = new EventEmitter < boolean > ( false ) ;
@@ -62,7 +59,7 @@ export class TypeaheadDirective implements OnInit {
6259 private placement :string = 'bottom-left' ;
6360 private popup :Promise < ComponentRef < any > > ;
6461
65- private cd : NgModel ;
62+ private ngControl : NgControl ;
6663 private viewContainerRef :ViewContainerRef ;
6764 private element :ElementRef ;
6865 private renderer :Renderer ;
@@ -142,10 +139,10 @@ export class TypeaheadDirective implements OnInit {
142139 }
143140 }
144141
145- public constructor ( cd : NgModel , viewContainerRef :ViewContainerRef , element :ElementRef ,
142+ public constructor ( control : NgControl , viewContainerRef :ViewContainerRef , element :ElementRef ,
146143 renderer :Renderer , loader :DynamicComponentLoader ) {
147144 this . element = element ;
148- this . cd = cd ;
145+ this . ngControl = control ;
149146 this . viewContainerRef = viewContainerRef ;
150147 this . renderer = renderer ;
151148 this . loader = loader ;
@@ -176,8 +173,8 @@ export class TypeaheadDirective implements OnInit {
176173 let valueStr :string = ( ( typeof value === 'object' && this . typeaheadOptionField )
177174 ? value [ this . typeaheadOptionField ]
178175 : value ) . toString ( ) ;
179- this . cd . viewToModelUpdate ( valueStr ) ;
180- setProperty ( this . renderer , this . element , 'value' , valueStr ) ;
176+ this . ngControl . viewToModelUpdate ( valueStr ) ;
177+ ( this . ngControl . control as FormControl ) . updateValue ( valueStr ) ;
181178 this . hide ( ) ;
182179 }
183180
@@ -204,8 +201,8 @@ export class TypeaheadDirective implements OnInit {
204201 this . container . parent = this ;
205202 // This improves the speedas it won't have to be done for each list item
206203 let normalizedQuery = ( this . typeaheadLatinize
207- ? TypeaheadUtils . latinize ( this . cd . model )
208- : this . cd . model ) . toString ( )
204+ ? TypeaheadUtils . latinize ( this . ngControl . control . value )
205+ : this . ngControl . control . value ) . toString ( )
209206 . toLowerCase ( ) ;
210207 this . container . query = this . typeaheadSingleWords
211208 ? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
@@ -325,8 +322,8 @@ export class TypeaheadDirective implements OnInit {
325322 if ( this . container && this . _matches . length > 0 ) {
326323 // This improves the speedas it won't have to be done for each list item
327324 let normalizedQuery = ( this . typeaheadLatinize
328- ? TypeaheadUtils . latinize ( this . cd . model )
329- : this . cd . model ) . toString ( )
325+ ? TypeaheadUtils . latinize ( this . ngControl . control . value )
326+ : this . ngControl . control . value ) . toString ( )
330327 . toLowerCase ( ) ;
331328 this . container . query = this . typeaheadSingleWords
332329 ? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
0 commit comments