<!DOCTYPE html>
<html>
<head>
<base href="." />
<script type="text/javascript" charset="utf-8">
window.AngularVersionForThisPlunker = 'latest'
</script>
<title>angular playground</title>
<link rel="stylesheet" href="style.css" />
<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>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* Styles go here */
.box{
height: 200px;
border: 1px solid red;
overflow:scroll;
}
### Angular Starter Plunker - Typescript
var angularVersion;
if(window.AngularVersionForThisPlunker === 'latest'){
angularVersion = ''; //picks up latest
}
else {
angularVersion = '@' + window.AngularVersionForThisPlunker;
}
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'+ angularVersion + '/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common' + angularVersion + '/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http' + angularVersion + '/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router' + angularVersion +'/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms' + angularVersion + '/bundles/forms.umd.js',
'@angular/animations': 'npm:@angular/animations' + angularVersion + '/bundles/animations.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations' + angularVersion + '/bundles/animations-browser.umd.js',
'@angular/core/testing': 'npm:@angular/core' + angularVersion + '/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http' + angularVersion + '/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router' + angularVersion + '/bundles/router-testing.umd.js',
'tslib': 'npm:tslib@1.6.1',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.2.1/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, VERSION} from '@angular/core'
@Component({
selector: 'my-app',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
// Set the model value
myValue = 1;
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from "@angular/common";
import { HttpModule, Http } from "@angular/http";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import {AppComponent} from './app.component';
import {PercentComponent} from './percent.component';
@NgModule({
imports: [
BrowserModule,
CommonModule,
HttpModule,
FormsModule,
ReactiveFormsModule
],
declarations: [ AppComponent, PercentComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
<percent-input [(ngModel)]="myValue"></percent-input>
<br/>
<span>Form Value {{myValue}}</span>
<div>
<input
#percentInput
type="number"
[disabled]="disabled"
[attr.disabled]="disabled"
class="form-control"
[(ngModel)]="wholeNumber"
(keydown)="onKeyDown($event)"
min="0"
max="100"
step="1" />
<br/>
<span>Control Value {{value}}</span>
<br/>
<span>Inner Value {{percent}}</span>
<br/>
<span>Formatted Value {{wholeNumber}}</span>
</div>
// -----------------------------------------------------------------------
// <copyright file="precent.component.ts" company="">
// </copyright>
// <summary>
// Defines the percent component, wraps the input control to handle all the percent conversion
// </summary>
// -----------------------------------------------------------------------
// Import the angular modules
import { Component, ViewChild } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
// <summary>
// Enumeration of keyboard key codes
// </summary>
enum KeyCode {
E = 69,
Add = 107,
Subtract = 109,
DecimalPoint = 110,
Dash = 189,
Period = 190,
}
@Component({
selector: "percent-input",
templateUrl: "./src/percent.component.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: PercentComponent,
multi: true
}
]
})
export class PercentComponent implements ControlValueAccessor {
// <summary>
// Internal value of the percent component
// </summary>
percent: number;
// <summary>
// Internal minimum value
// </summary>
minValue = 0;
// <summary>
// Internal maximum value
// </summary>
maxValue = 100;
// <summary>
// Reference to the value propage change function
// </summary>
propagateChange = (_: any) => { };
// <summary>
// Change event when the map view has been changed
// </summary>
@ViewChild("percentInput") inputControl;
// <summary>
// Get and set the control formated value for display
// </summary>
get wholeNumber(): number {
// Null check
if (this.percent == null) {
return null;
}
return Math.round(this.percent * 100);
}
set wholeNumber(v: number) {
// Null check
if (v != null) {
if(v >= this.minValue && v <= this.maxValue) {
this.percent = Number((v / 100).toFixed(2));
}else{
this.inputControl.nativeElement.value = this.wholeNumber;
}
} else {
this.percent = v;
}
// Call the propage change function
this.propagateChange(this.percent);
}
// <summary>
// Control get value accessor
// </summary>
// get accessor
get value(): number {
return this.percent;
}
// <summary>
// Control set value accessor
// </summary>
set value(v: number) {
this.percent = v;
// Call the propage change function
this.propagateChange(this.percent);
}
// <summary>
// Onkey down event handler
// </summary>
onKeyDown(event){
// Prevent the following keys
if(event.keyCode === KeyCode.Period ||
event.keyCode === KeyCode.DecimalPoint ||
event.keyCode === KeyCode.E ||
event.keyCode === KeyCode.Add ||
event.keyCode === KeyCode.Dash ||
event.keyCode === KeyCode.Subtract){
event.preventDefault();
}
}
// <summary>
// Control write value
// </summary>
writeValue(v: number) {
// If the model is cleared, we want the look up control to reset the values to empty
if (v == null) {
v = null;
}
this.percent = v;
}
// <summary>
// Control register on change function
// </summary>
registerOnChange(fn) {
this.propagateChange = fn;
}
// <summary>
// Control register on touch function
// </summary>
registerOnTouched() {
}
}