In Angular2, you can use the same approach as @Tyler's answer, but with a new syntax.
<div class="form-group" [ngClass]="{ 'has-success': user.valid, 'has-error': user.invalid}" > <input type="text" class="form-control" id="usr" name="usr" required #user="ngModel" [(ngModel)]="model.usr" > </div>
You create a new variable called user , line #user="ngModel" , which contains the state of the model for user input. And with [ngClass]="{ 'class-name': bool-expression}" you assign classes when expressions become true.
Keep in mind that you need to put class names as strings if they are not a valid JavaScript identifier (for example, they have - ).
source share