What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
Model answer
ngClass adds or removes CSS classes based on a condition or an object, and ngStyle sets inline styles from an object. They still work, but for simple cases Angular's direct bindings are lighter: [class.active]="isActive" toggles one class, and [style.width.px]="width" sets one style.
Use class and style bindings for one or two values, and ngClass or ngStyle when you need several at once.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is a pipe? Name some built-in pipes.
- How do you create a custom pipe? What is the difference between pure and impure pipes?
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?
- What is the difference between providedIn root and providing a service in a component?