|
1 | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
|
2 | 2 | import { NguiAutoCompleteService } from './auto-complete.service';
|
| 3 | +import { Observable } from 'rxjs'; |
3 | 4 |
|
4 | 5 | @Component({
|
5 | 6 | selector: 'ngui-auto-complete',
|
@@ -132,39 +133,39 @@ export class NguiAutoCompleteComponent implements OnInit {
|
132 | 133 | this.filteredList = this.filteredList.slice(0, this.maxNumList);
|
133 | 134 | }
|
134 | 135 |
|
135 |
| - } else { // remote source |
| 136 | + } else {// remote source |
136 | 137 | this.isLoading = true;
|
137 | 138 |
|
138 | 139 | if (typeof this.source === 'function') {
|
139 | 140 | // custom function that returns observable
|
140 |
| - this.source(keyword).subscribe( |
141 |
| - (resp) => { |
142 |
| - |
143 |
| - if (this.pathToData) { |
144 |
| - const paths = this.pathToData.split('.'); |
145 |
| - paths.forEach((prop) => resp = resp[prop]); |
146 |
| - } |
147 |
| - |
148 |
| - this.filteredList = resp; |
149 |
| - if (this.maxNumList) { |
150 |
| - this.filteredList = this.filteredList.slice(0, this.maxNumList); |
151 |
| - } |
152 |
| - }, |
153 |
| - (error) => console.warn(error), |
154 |
| - () => this.isLoading = false // complete |
| 141 | + (this.source(keyword) as Observable<any>).subscribe({ |
| 142 | + next: (resp) => { |
| 143 | + if (this.pathToData) { |
| 144 | + const paths = this.pathToData.split('.'); |
| 145 | + paths.forEach((prop) => resp = resp[prop]); |
| 146 | + } |
| 147 | + |
| 148 | + this.filteredList = resp; |
| 149 | + if (this.maxNumList) { |
| 150 | + this.filteredList = this.filteredList.slice(0, this.maxNumList); |
| 151 | + } |
| 152 | + }, |
| 153 | + error: (error) => console.warn(error), |
| 154 | + complete: () => this.isLoading = false |
| 155 | + } |
155 | 156 | );
|
156 | 157 | } else {
|
157 | 158 | // remote source
|
158 |
| - |
159 |
| - this.autoComplete.getRemoteData(keyword).subscribe((resp) => { |
| 159 | + this.autoComplete.getRemoteData(keyword).subscribe({ |
| 160 | + next: (resp) => { |
160 | 161 | this.filteredList = resp ? resp : [];
|
161 | 162 | if (this.maxNumList) {
|
162 | 163 | this.filteredList = this.filteredList.slice(0, this.maxNumList);
|
163 | 164 | }
|
164 | 165 | },
|
165 |
| - (error) => console.warn(error), |
166 |
| - () => this.isLoading = false // complete |
167 |
| - ); |
| 166 | + error: (error) => console.warn(error), |
| 167 | + complete: () => this.isLoading = false |
| 168 | + }); |
168 | 169 | }
|
169 | 170 | }
|
170 | 171 | }
|
|
0 commit comments