@@ -2,12 +2,12 @@ import {
2
2
Directive , Input , Output , HostListener , EventEmitter , OnInit , ElementRef ,
3
3
Renderer , DynamicComponentLoader , ComponentRef , ReflectiveInjector , provide , ViewContainerRef
4
4
} from '@angular/core' ;
5
- import { NgModel } from '@angular/forms' ;
5
+ import { NgControl , FormControl } from '@angular/forms' ;
6
6
import { TypeaheadUtils } from './typeahead-utils' ;
7
7
import { TypeaheadContainerComponent } from './typeahead-container.component' ;
8
8
import { TypeaheadOptions } from './typeahead-options.class' ;
9
9
10
- import { Observable } from 'rxjs/Rx ' ;
10
+ import { Observable } from 'rxjs/Observable ' ;
11
11
12
12
import 'rxjs/add/observable/from' ;
13
13
import 'rxjs/add/operator/debounceTime' ;
@@ -21,13 +21,10 @@ import {global} from '@angular/core/src/facade/lang';
21
21
const KeyboardEvent = ( global as any ) . KeyboardEvent as KeyboardEvent ;
22
22
/* tslint:enable */
23
23
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
-
29
24
@Directive ( {
30
- selector : '[typeahead][ngModel]'
25
+ /* tslint:disable */
26
+ selector : '[typeahead][ngModel],[typeahead][formControlName]'
27
+ /* tslint:enable */
31
28
} )
32
29
export class TypeaheadDirective implements OnInit {
33
30
@Output ( ) public typeaheadLoading :EventEmitter < boolean > = new EventEmitter < boolean > ( false ) ;
@@ -62,7 +59,7 @@ export class TypeaheadDirective implements OnInit {
62
59
private placement :string = 'bottom-left' ;
63
60
private popup :Promise < ComponentRef < any > > ;
64
61
65
- private cd : NgModel ;
62
+ private ngControl : NgControl ;
66
63
private viewContainerRef :ViewContainerRef ;
67
64
private element :ElementRef ;
68
65
private renderer :Renderer ;
@@ -142,10 +139,10 @@ export class TypeaheadDirective implements OnInit {
142
139
}
143
140
}
144
141
145
- public constructor ( cd : NgModel , viewContainerRef :ViewContainerRef , element :ElementRef ,
142
+ public constructor ( control : NgControl , viewContainerRef :ViewContainerRef , element :ElementRef ,
146
143
renderer :Renderer , loader :DynamicComponentLoader ) {
147
144
this . element = element ;
148
- this . cd = cd ;
145
+ this . ngControl = control ;
149
146
this . viewContainerRef = viewContainerRef ;
150
147
this . renderer = renderer ;
151
148
this . loader = loader ;
@@ -176,8 +173,8 @@ export class TypeaheadDirective implements OnInit {
176
173
let valueStr :string = ( ( typeof value === 'object' && this . typeaheadOptionField )
177
174
? value [ this . typeaheadOptionField ]
178
175
: 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 ) ;
181
178
this . hide ( ) ;
182
179
}
183
180
@@ -204,8 +201,8 @@ export class TypeaheadDirective implements OnInit {
204
201
this . container . parent = this ;
205
202
// This improves the speedas it won't have to be done for each list item
206
203
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 ( )
209
206
. toLowerCase ( ) ;
210
207
this . container . query = this . typeaheadSingleWords
211
208
? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
@@ -325,8 +322,8 @@ export class TypeaheadDirective implements OnInit {
325
322
if ( this . container && this . _matches . length > 0 ) {
326
323
// This improves the speedas it won't have to be done for each list item
327
324
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 ( )
330
327
. toLowerCase ( ) ;
331
328
this . container . query = this . typeaheadSingleWords
332
329
? TypeaheadUtils . tokenize ( normalizedQuery , this . typeaheadWordDelimiters , this . typeaheadPhraseDelimiters )
0 commit comments