最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - How to add multiple classes with host property? - Stack Overflow

programmeradmin4浏览0评论

component.scss

:host {
  //some css props and values
}

:host.xyz {
  //some props and values
}

:host.pqr {
  //some props and values
}

component.ts

@Component({
//all component's metadata added.
  host: {
    class: 'xyz', // how do i use class pqr here ?
  }
})
export class Component {
  type = input.required<String>(); //signal
}

component.html

<ng-content />

Problem: I want to use multiple classes in the host property which is an alternative to @HostBinding()

Alernatively: I tried using the other way with the use of signals below:

adding only the host property part.

host: { '[class.xyz]': 'type === "xyz"', '[class.pqr]': 'type === "pqr"' }

With the above piece of code, it does not apply the class because the time the class is applied the input property does not get its value.

Expecting - The classes should be applied to the host element when we pass the type inputs based on type = xyz or type = pqr as strings.

component.scss

:host {
  //some css props and values
}

:host.xyz {
  //some props and values
}

:host.pqr {
  //some props and values
}

component.ts

@Component({
//all component's metadata added.
  host: {
    class: 'xyz', // how do i use class pqr here ?
  }
})
export class Component {
  type = input.required<String>(); //signal
}

component.html

<ng-content />

Problem: I want to use multiple classes in the host property which is an alternative to @HostBinding()

Alernatively: I tried using the other way with the use of signals below:

adding only the host property part.

host: { '[class.xyz]': 'type === "xyz"', '[class.pqr]': 'type === "pqr"' }

With the above piece of code, it does not apply the class because the time the class is applied the input property does not get its value.

Expecting - The classes should be applied to the host element when we pass the type inputs based on type = xyz or type = pqr as strings.

Share Improve this question edited Jan 20 at 11:09 Naren Murali 56.4k5 gold badges40 silver badges71 bronze badges asked Jan 20 at 11:00 AdityaAditya 2,5367 gold badges38 silver badges64 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You have to execute the signal (type()) inside the conditional of the host:

  @Component({
    selector: 'app-child',
    template: `
      <a target="_blank" href="https://angular.dev/overview">
        Learn more about Angular
      </a>
    `,
    host: {
      '[class.xyz]': 'type() === "xyz"',
      '[class.pqr]': 'type() === "pqr"',
    },
  })
  export class Child {
    type = input.required<string>(); //signal
  }

Full Code:

CSS:

.xyz {
  background-color: red !important;
}
.pqr {
  background-color: blue !important;
}

TS:

import { Component, input } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

@Component({
  selector: 'app-child',
  template: `
      <a target="_blank" href="https://angular.dev/overview">
        Learn more about Angular
      </a>
    `,
  host: {
    '[class.xyz]': 'type() === "xyz"',
    '[class.pqr]': 'type() === "pqr"',
  },
})
export class Child {
  type = input.required<string>(); //signal
}

@Component({
  selector: 'app-root',
  imports: [Child],
  template: `
    <app-child type="xyz"/><br/>
    <app-child type="pqr"/><br/>
  `,
})
export class App {
  name = 'Angular';
}

bootstrapApplication(App);

Stackblitz Demo

发布评论

评论列表(0)

  1. 暂无评论