<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular playground</title>
<link href="https://unpkg.com/@ng-select/ng-select/themes/default.theme.css" rel="stylesheet">
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css">
<script src="https://use.fontawesome.com/7d64cb55fa.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<select-autocomplete>
loading...
</select-autocomplete>
</body>
</html>
import {Component, NgModule, EventEmitter, ChangeDetectorRef} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import {NgSelectModule, NgOption} from '@ng-select/ng-select';
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {catchError, map, debounceTime, switchMap} from 'rxjs/operators';
import {of} from 'rxjs/observable/of';
@Component({
selector: 'select-autocomplete',
template: `
<label>Search with autocomplete in Github accounts</label>
<ng-select [items]="items"
[multiple]="true"
[addTag]="true"
bindLabel="login"
placeholder="Find your Github account"
[typeahead]="typeahead"
[(ngModel)]="githubAccount">
<ng-template ng-option-tmp let-item="item">
<img [src]="item.avatar_url" width="20px" height="20px"> {{item.login}}
</ng-template>
</ng-select>
<p>
Selected github account:
<span *ngIf="githubAccount">
<img [src]="githubAccount.avatar_url" width="20px" height="20px"> {{githubAccount.login}}
</span>
</p>
`
})
export class SelectAutocompleteComponent {
githubAccount: any;
items = [];
typeahead = new EventEmitter<string>();
constructor(private http: HttpClient, private cd: ChangeDetectorRef) {
this.typeahead
.pipe(
debounceTime(200),
switchMap(term => this.loadGithubUsers(term))
)
.subscribe(items => {
this.items = items;
this.cd.markForCheck();
}, (err) => {
console.log('error', err);
this.items = [];
this.cd.markForCheck();
});
}
loadGithubUsers(term: string): Observable<any[]> {
return this.http.get<any>(`https://api.github.com/search/users?q=${term}`).pipe(
catchError(() => of(({items: []}))),
map(rsp => rsp.items),
);
}
}
@NgModule({
imports: [
BrowserModule,
FormsModule,
NgSelectModule,
HttpClientModule
],
declarations: [ SelectAutocompleteComponent ],
bootstrap: [ SelectAutocompleteComponent ]
})
export class AppModule {}
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@ng-select/ng-select': 'npm:@ng-select/ng-select/bundles/ng-select.umd.js',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.2.1/lib/typescript.js',
'tslib': 'npm:tslib'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
'@ng-select/ng-select': {
defaultExtension: 'js'
}
}
});
body {
padding: 10px;
}
p {
margin-top:20px;
}
label {
margin-top: 20px;
}