import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<h1>Hello {{name}}</h1>
<p-typeahead>
<select style="width: 200px;">
<option value=""></option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5" selected>Five</option>
</select>
</p-typeahead>
<hr>
ngModel support:
<p-typeahead [(ngModel)]="address">
<select style="width: 200px;">
<option value=""></option>
<option value="btl">btl - Bantul</option>
<option value="kp">kp - Kulon Progo</option>
<option value="slm">slm - Sleman</option>
<option value="gk">gk - Gunung Kidul</option>
<option value="kt">kt - Kota</option>
</select>
</p-typeahead>
address: {{address}}
`
})
export class AppComponent {
address='kp';
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { CmpTypeahead } from './CmpTypeahead';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, CmpTypeahead ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
<!DOCTYPE html>
<html>
<head>
<title>Angular Quickstart</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {color:#369;font-family: Arial,Helvetica,sans-serif;}
</style>
<!-- Polyfills -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('main.js').catch(function(err){ console.error(err); });
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<my-app>Loading AppComponent content here ...</my-app>
</body>
</html>
/**
* WEB ANGULAR VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.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/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs@5.0.1',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.0.10/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
import {Component, Input, AfterViewInit, OnChanges, SimpleChange, forwardRef, ViewChild, ElementRef} from '@angular/core';
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from '@angular/forms';
export const TYPEAHEAD_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CmpTypeahead),
multi: true
};
declare var jQuery: any;
@Component({
moduleId: module.id,
selector: 'p-typeahead',
template: `<ng-content></ng-content>`,
providers: [TYPEAHEAD_VALUE_ACCESSOR],
styleUrls: ['typeahead.css']
})
export class CmpTypeahead implements AfterViewInit, ControlValueAccessor {
private objRef: any;
@Input() items: any;
value: string;
onModelChange: Function = () => {};
onModelTouched: Function = () => {};
@Input() disabled: any;
@Input() placeholder = '';
@Input() allowClear = true;
selectElm: any;
initItemsCount = 0;
constructor(private elRef: ElementRef) {
}
ngAfterViewInit() {
this.selectElm = this.elRef.nativeElement.getElementsByTagName('select')[0];
this.initItemsCount = this.selectElm.options.length;
this.objRef = jQuery(this.selectElm);
this.objRef.select2({
placeholder: this.placeholder,
allowClear: this.allowClear
});
this.objRef.on("select2:select", (e) => {
this.value = this.objRef.val();
this.onModelChange(this.value);
this.selectElm.focus();
});
this.objRef.on("select2:unselect", (e) => {
this.value = null;
this.onModelChange(this.value);
this.selectElm.focus();
});
}
writeValue(value: any): void {
if (value === undefined) {
value = null;
}
this.value = value;
if (this.objRef) {
this.objRef.val(value).trigger("change");
}
}
ngAfterViewChecked() {
if (this.selectElm && this.selectElm.options.length != this.initItemsCount) {
/**
* trigger change in writeValue() doesn't work if the items (the option tags) in
* <select> haven't been rendered. This trigger is to fix this problem
*/
this.initItemsCount = this.selectElm.options.length;
this.objRef.val(this.value).trigger("change");
}
}
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
setDisabledState(val: boolean): void {
this.disabled = val;
this.objRef.prop("disabled", val);
}
}
:host {
display: block;
border-radius: 4px;
/*padding-left: -5px;*/
}
:host[required] /deep/ .select2-selection--single {
border-radius: 0 4px 4px 0;
}