Skip to content

Commit

Permalink
feat(forms): add hasError and getError to AbstractControlDirective
Browse files Browse the repository at this point in the history
Allows cleaner expressions in template-driven forms.

Before:

    <label>Username</label><input name="username" ngModel required #username="ngModel">
    <div *ngIf="username.dirty && username.control.hasError('required')">Username is required</div>

After:

    <label>Username</label><input name="username" ngModel required #username="ngModel">
    <div *ngIf="username.dirty && username.hasError('required')">Username is required</div>

Fixes angular#7255
  • Loading branch information
cexbrayat committed Sep 29, 2016
1 parent 36bc2ff commit e231072
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ export abstract class AbstractControlDirective {
reset(value: any = undefined): void {
if (isPresent(this.control)) this.control.reset(value);
}

hasError(errorCode: string, path: string[] = null): boolean {
return isPresent(this.control) ? this.control.hasError(errorCode, path) : false;
}

getError(errorCode: string, path: string[] = null): any {
return isPresent(this.control) ? this.control.getError(errorCode, path) : null;
}
}

0 comments on commit e231072

Please sign in to comment.