import { Component } from "@angular/core";
@Component({
selector: 'app-root',
template: `
<h1>My Angular 2 App</h1>
<app-number></app-number>
`
})
export class AppComponent {}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { NumberMaskPipe } from './number-mask.pipe';
import { PhoneNumberComponent } from './phone-number.component';
@NgModule({
imports: [BrowserModule,FormsModule],
providers: [],
declarations: [
AppComponent,
NumberMaskPipe,
PhoneNumberComponent
],
bootstrap: [AppComponent]
})
export class AppModule {}
<!DOCTYPE html>
<html>
<head>
<script>document.write('<base href="' + document.location + '" />');</script>
<title>NgModule Deluxe</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
<script src="https://npmcdn.com/zone.js@0.6.25?main=browser"></script>
<script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
<script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('main.ts').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<app-root>Loading...</app-root>
</body>
</html>
/**
* PLUNKER VERSION (based on systemjs.config.js in angular.io)
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function(global) {
var ngVer = '@2.3.0'; // lock in the angular package version; do not let it float to current!
var routerVer = '@3.3.0'; // lock router version
var formsVer = '@2.3.0'; // lock forms version
var routerDeprecatedVer = '@2.0.0-rc.2'; // temporarily until we update all the guides
//map tells the System loader where to look for things
var map = {
'app': 'app',
'@angular': 'https://npmcdn.com/@angular', // sufficient if we didn't pin the version
'@angular/router': 'https://npmcdn.com/@angular/router' + routerVer,
'@angular/forms': 'https://npmcdn.com/@angular/forms' + formsVer,
'@angular/router-deprecated': 'https://npmcdn.com/@angular/router-deprecated' + routerDeprecatedVer,
'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.12',
'ts': 'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'https://npmcdn.com/typescript@2.0.2/lib/typescript.js',
};
//packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.ts', defaultExtension: 'ts' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
var ngPackageNames = [
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'upgrade',
];
// Add map entries for each angular package
// only because we're pinning the version with `ngVer`.
ngPackageNames.forEach(function(pkgName) {
map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
});
// Add package entries for angular packages
ngPackageNames.concat(['forms', 'router', 'router-deprecated']).forEach(function(pkgName) {
// Bundled (~40 requests):
packages['@angular/'+pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
// Individual files (~300 requests):
//packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
var config = {
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
map: map,
packages: packages
};
System.config(config);
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'mask'
})
export class NumberMaskPipe implements PipeTransform {
transform(phoneNumber: string): string {
const visibleDigits = 4;
let maskedSection = phoneNumber.slice(0, -visibleDigits);
let visibleSection = phoneNumber.slice(-visibleDigits);
return maskedSection.replace(/./g, '*') + visibleSection;
}
}
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-number',
template: `
<input type="text" #phoneNumberText name="phoneNumber" id="phoneNumber" [ngModel]="phoneNumberUI" (keyup)="newmethod($event)"/>
<p>Your phone number is: {{ phoneNumberUI }}</p>
<p>Your original Number is {{phoneNumber}}</p>
`
})
export class PhoneNumberComponent implements OnInit {
phoneNumberUI: string;
phoneNumber: string;
maskedSection: any;
visibleSection: any;
maskedSectionOriginal: string = "";
previouslength: number = 0;
@ViewChild('phoneNumberText') private phoneNumberValue: ElementRef;
constructor() { }
ngOnInit() {
}
newmethod(event) {
if (event.code == "Backspace" || event.code == "Delete") {
this.clearData();
}
else {
var temp=JSON.parse(JSON.stringify(this.phoneNumberValue.nativeElement.value));
temp= temp.replace(/\*/g, '');
console.log(temp);
console.log(this.phoneNumberValue.nativeElement.value);
if(!isNaN(temp))
this.maskData(this.phoneNumberValue.nativeElement.value)
else
this.clearData();
}
}
maskData(event) {
if (event.length > 4) {
let visibleDigitstemp = 4 - event.length;
let maskedSectiontemp = event.slice(0, -visibleDigitstemp);
if (maskedSectiontemp)
maskedSectiontemp = maskedSectiontemp.replace(/\*/g, '');
if (maskedSectiontemp.length > 0) {
this.maskedSectionOriginal = this.maskedSectionOriginal + JSON.parse(JSON.stringify(maskedSectiontemp));
}
}
const visibleDigits = 4;
this.maskedSection = event.slice(0, -visibleDigits);
this.visibleSection = event.slice(-visibleDigits);
this.phoneNumberUI = this.maskedSection.replace(/./g, '*') + this.visibleSection;
this.phoneNumber = this.maskedSectionOriginal + this.visibleSection;
}
clearData() {
this.maskedSection = "";
this.visibleSection = "";
this.phoneNumberUI = "";
this.phoneNumber = "";
this.maskedSectionOriginal="";
}
}
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { enableProdMode } from '@angular/core';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);