1

When editing my component, the column grids also are changed.

Currently, I have this for my edit-component.html

      <div class="col-6" *ngIf="!edit">
        Column1
      </div>
      <div class="col-12 col-md-4 col-sm-2" *ngIf="edit">
        Column1
      </div>
      <div class="col-6" *ngIf="!edit">
        Column2
      </div>
      <div class="col-12 col-md-4 col-sm-2" *ngIf="edit">
        Column2
      </div>

And I have this for edit-component.ts

    edit: boolean = false;

This method works and the css is changing when editting or readonly. But is there other way that I can have if condition for col-12 col-md-5 and col-6?

2 Answers 2

2

You can use the ngClass directive.

<div [ngClass]="{'col-6': !edit, 'col-sm-2 col-md-4 col-12': edit}">
    Column1
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

The only adjustment I'd suggest, is to extract that out to a getter, so that you can take advantage of re-use. Having that defined on every column element will be a mess.
Fair point about re-use, however I wouldn't use a getter here because they're executed whenever angular runs change detection. Since this def would make the template harder to read, I'd actually consider extracting this into a pipe. Which also makes it more testable.
ngClass directive is the way. I just edited how it looks but it works when using ngClass like [ngClass]="edit ? 'col-12 col-md-4 col-sm-2' : 'col-6'"
@newProgrammer005 that works, just because it works with simple strings. But the more powerful way of using ngClass involves creating an object like this, so that you can dynamically apply n number of classes based on different conditions. This illustrates that capability more. You may also consider the approach I suggested below, which allows for better re-use.
0

Yes. Using the NgClass directive, you can apply classes dynamically based on conditions in your typescript class.

For example: Typescript:

get columnClassDef() {
  return {
    'col-6': !this.edit,
    'col-12 col-md-4 col-sm-2': this.edit,
  };
}

Html:

<div [ngClass]="columnClassDef">
  Column1
</div>
<div [ngClass]="columnClassDef">
  Column2
</div>

NgClass will apply the key of the object as a css class on the element, if the value evaluates to true. It doesn't add the class if the value evaluates to false.

3 Comments

This definitely makes the template more readable, but getColumnClassDef will be re-executed with every change detection cycle.
@spots It would if it was inline in the template, though, too, wouldn't it? I think a pure pipe is the only way around that.
TBH, I'm not sure. The edit variable is the only thing that needs to be evaluated, so I guess technically yes there's work being done. In this example however a new object is created every time. It's unlikely to create a problem in small apps, but once there's lots of components angular has to manage it could become death by papercuts! Like you said, a pure pipe is def the way to go IMO.

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.