Search code examples
typescriptangularng-class

Angular2 is there a way to set bootstrap col dynamically?


Is it possible to use an If statement to set the column width of the div? I have got different div elements like this:

    <div class="col-xs-3">
        <select class="form-control" [(ngModel)]="selectedPrototypeSelector" (ngModelChange)="onPrototypeChange()">
            <option *ngFor="#p of prototypes" [value]="p.selector">
                {{ p.selectorName }}
            </option>
        </select>
    </div>

    <div class="col-xs-1" *ngIf="prototype?.valueType === 'Set'">
       <p class="text-center">If</p>
   </div>

How can I change the col-xs-3 above to col-xs-2 for example when prototype?.valueType === 'Set'


Solution

  • Use ngClass:

    <div [ngClass]="{
        'col-xs-3': prototype?.valueType !== 'Set',
        'col-xs-2': prototype?.valueType === 'Set'}"> 
    

    See also https://angular.io/docs/ts/latest/api/common/NgClass-directive.html