Can I use ngClass on the ng-template?

See original GitHub issue

Id like to add a class to a menu item conditionally.

<ng-template contextMenuItem (execute)="doSomething($event.item)" [ngClass]="{'first': true}">

but get a console error: TypeError: Cannot read property ‘add’ of undefined

Even better, I’d like to reference the item’s properties, like [ngClass]="{'first': $event.item.isSnapToGrid}"

but I get: ReferenceError: $event is not defined

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
isaacplmanncommented, Feb 16, 2018

You can’t put element attributes on an <ng-template> since it doesn’t map to an actual DOM element. The best you can do is this:

<ng-template contextMenuItem (execute)="doSomething($event.item)">
  <div [ngClass]="{'first': true}"></div>
</ng-template>

If all you want to do is write a css rule for the first context menu item, you can do that without a class.

.ngx-contextmenu li:first-child {
  background-color:red;
}
0reactions
demonguru18commented, Sep 20, 2019

In you css file => .loading { margin: 10% 0 0 50%; }

In the HTML file =>

<ng-template #loading>
  <div class="loading">
    <img  src="/images/spinner.gif" />
  </div>
</ng-template>

OR In you css file => .loading { margin: 10% 0 0 50%; }

In the HTML file =>

<ng-template #loading>
  <div [class.loading]="product.loading">
    <img  src="/images/spinner.gif" />
  </div>
</ng-template>

In the component file .ts

/*Declare property*/
product : Product;

/*Initialize property*/
ngOnInit() { 
this.product = {"name" : "iphone",  "loading" : true};
}


In the Interface file declare custom type property

export interface Product {
  name : string;
  loading : boolean; 
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style angular ng-template selector - Stack Overflow
using #other_content as ID in my .css file; adding a class to <ng-template>; styling all <td> tags. It's not working and after ...
Read more >
Angular ngClass and ngStyle: The Complete Guide
The ngClass directive will take an expression that will be used to determine which state styles to apply at a given time to...
Read more >
Angular NgClass Example – How to Add Conditional CSS ...
ngClass is a directive in Angular [https://angular.io/api/common/NgClass] that adds and removes CSS classes on an HTML element.
Read more >
NgStyle & NgClass • Angular - codecraft.tv
The NgClass directive allows you to set the CSS class dynamically for a DOM element. Tip. The NgClass directive will feel very similar...
Read more >
NgClass - Angular
string - the CSS classes listed in the string (space delimited) are added, · Array - the CSS classes declared as Array elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found