refactor(playground): make playground great again
This commit is contained in:

committed by
Victor Berchet

parent
69f87ca075
commit
3d9d839c6c
@ -8,8 +8,7 @@
|
||||
|
||||
|
||||
import {Component, Directive, Host, NgModule} from '@angular/core';
|
||||
import {isPresent, print} from '@angular/core/src/facade/lang';
|
||||
import {FormGroup, FormsModule, NG_VALIDATORS, NgForm} from '@angular/forms';
|
||||
import {FormControl, FormGroup, FormsModule, NG_VALIDATORS, NgForm} from '@angular/forms';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
||||
|
||||
@ -33,8 +32,8 @@ class CheckoutModel {
|
||||
/**
|
||||
* Custom validator.
|
||||
*/
|
||||
function creditCardValidator(c: any /** TODO #9100 */): {[key: string]: boolean} {
|
||||
if (isPresent(c.value) && /^\d{16}$/.test(c.value)) {
|
||||
function creditCardValidator(c: FormControl): {[key: string]: boolean} {
|
||||
if (c.value && /^\d{16}$/.test(c.value)) {
|
||||
return null;
|
||||
} else {
|
||||
return {'invalidCreditCard': true};
|
||||
@ -74,17 +73,17 @@ class CreditCardValidator {
|
||||
`
|
||||
})
|
||||
class ShowError {
|
||||
formDir: any /** TODO #9100 */;
|
||||
formDir: NgForm;
|
||||
controlPath: string;
|
||||
errorTypes: string[];
|
||||
|
||||
constructor(@Host() formDir: NgForm) { this.formDir = formDir; }
|
||||
|
||||
get errorMessage(): string {
|
||||
var form: FormGroup = this.formDir.form;
|
||||
var control = form.get(this.controlPath);
|
||||
if (isPresent(control) && control.touched) {
|
||||
for (var i = 0; i < this.errorTypes.length; ++i) {
|
||||
const form: FormGroup = this.formDir.form;
|
||||
const control = form.get(this.controlPath);
|
||||
if (control && control.touched) {
|
||||
for (let i = 0; i < this.errorTypes.length; ++i) {
|
||||
if (control.hasError(this.errorTypes[i])) {
|
||||
return this._errorMessage(this.errorTypes[i]);
|
||||
}
|
||||
@ -94,8 +93,11 @@ class ShowError {
|
||||
}
|
||||
|
||||
private _errorMessage(code: string): string {
|
||||
var config = {'required': 'is required', 'invalidCreditCard': 'is invalid credit card number'};
|
||||
return (config as any /** TODO #9100 */)[code];
|
||||
const config: {[key: string]: string} = {
|
||||
'required': 'is required',
|
||||
'invalidCreditCard': 'is invalid credit card number',
|
||||
};
|
||||
return config[code];
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,8 +165,8 @@ class TemplateDrivenForms {
|
||||
countries = ['US', 'Canada'];
|
||||
|
||||
onSubmit(): void {
|
||||
print('Submitting:');
|
||||
print(this.model);
|
||||
console.log('Submitting:');
|
||||
console.log(this.model);
|
||||
}
|
||||
}
|
||||
@NgModule({
|
||||
|
Reference in New Issue
Block a user