1

I have a page that will have a handful of input boxes that I want to be able to flag for

       <div class="form-group">
            <label class="d-block">Allowance Type</label>
            <div class="input-group">
                <input type="text" class="form-control width-150" [(ngModel)]="selectedPricingItem.allowanceTypeDescription" readonly>
                <span class="input-group-addon mg-batch-flag" (click)="flagItem('allowanceTypeDescription','Allowance Type')" data-toggle="modal" data-target="#auditDialog">
                     <i class="fa fa-fw fa-flag" aria-hidden="true">
                     </i>
                </span>
            </div>
        </div>

I have this structure a few times on the page and I want to be able to a bootstrap class (has-danger) to them based on a the click event handler I have. In that handler I am just populating an instance of a class that I intend on submitting to a web service at this point. Is the best approach to create a component with the different styles and use *ngIf to hide/show?

1 Answer 1

3

Just use a class binding

[class.has-danger]="propWithValue"

where propWithValue should have the value true or false to get the class added or removed. There is also [ngClass] for more complex scenarios like adding multiple classes.

Sign up to request clarification or add additional context in comments.

4 Comments

is this the best way to do it since i need to be able to control the class on multiple controls independently. this will make me have multiple variables to manage no?
If you want to have multiple elements with each their own status, then yes, you need multiple variables. Alternatively you can use an array - depending on your use case. But I don't know any other way. To store a status you need a variable.
I am thinking that a component with a property called "status" would be the cleanest. Only issue is that I would have multiple on same page
On the same page wouldn't matter. What matters is when it's within the same component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.