Angular 2 - Apply conditional style to a directive's child HTML element

Chris

I am trying to apply a class to an HTML element based on a click event. This works fine when setting the class property for the child component's selector from the parent component's template as seen by the following snippet from the parent component:

[class.bordered]='isSelected(item)'

This will appropriately set the style when that item is clicked. However, I want to set an inner HTML element's class in the child component based on the same sort of click event, here's the desired target for the style for the child component:

template: `
  <div class="This is where I really want to apply the style">  
    {{ item.val }}
  </div>
`

Is there a way to do this that is easily supported? Or is this considered a bad practice and I should design my components to avoid this sort of conditional styling situation?

Full code:

@Component({
  selector: 'parent-component',
  directives: [ChildComponent],
  template: `
    <child-component
      *ngFor='#item of items'
      [item]='item'
      (click)='clicked(item)'
      [class.bordered]='isSelected(item)'>
    </child-component>
  `
})
export class ParentComponent {
  items: Item[];
  currentItem: item;

  constructor(private i: ItemService) {
    this.items = i.items;
  }

  clicked(item: Item): void {
    this.currentItem = item;
  }

  isSelected(item: Items): boolean {
    if (!item || !this.currentItem) {
      return false;
    }
    return item.val === this.currentItem.val;
  }
}


@Component({
  selector: 'child-component',
  inputs: ['item'],
  template: `
    <div class="This is where I really want to apply the style">  
      {{ item.val }}
    </div>
  `
})
export class ChildComponent {}
Chris

I found a better way to solve this making good use of Angular2 features.

Specifically instead of doing trickery with :host and CSS functionality you can just pass in a variable to the child component by changing:

[class.bordered]='isSelected(item)'

being set in the element of the child class change it to

[isBordered]='isSelected(item)'

Then on the div you'd like to apply the bordered class to in the template of the child component just add:

[ngClass]='{bordered: isBordered}'

Here is the full code with the change:

@Component({
  selector: 'parent-component',
  directives: [ChildComponent],
  template: `
    <child-component
      *ngFor='#item of items'
      [item]='item'
      (click)='clicked(item)'
      [isBordered]='isSelected(item)'>
    </child-component>
  `
})
export class ParentComponent {
  items: Item[];
  currentItem: item;

  constructor(private i: ItemService) {
    this.items = i.items;
  }

  clicked(item: Item): void {
    this.currentItem = item;
  }

  isSelected(item: Items): boolean {
    if (!item || !this.currentItem) {
      return false;
    }
    return item.val === this.currentItem.val;
  }
}


@Component({
  selector: 'child-component',
  inputs: ['item'],
  template: `
    <div [ngClass]='{bordered: isBordered}'>
      {{ item.val }}
    </div>
  `
})
export class ChildComponent {}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Apply style to element when click outside of directive attribute element in Angular

Angular Directive not for child element

How to style a Child html element in typescript / angular

Angular 7 + Material - apply material "directive" on HTML5 element?

Apply style to child that are under certain element

Apply css style to child element of parent class

How to apply style to child of element with active state

MakeStyles (Material UI) apply style to child element

Apply conditional css style sheet for html editor

Angular 2, 2.0.0-rc.2, Cannot apply inline css3 transform with style directive

Angular directive passing arguments to controller function within element's .html()

Apply directive if element has Id or Class (angular)

Angular directive listen to element style change

Angular - HTML element not found in directive

Angular directive on parent to change class of a child element?

Angular button directive incorrectly appending child element

How do you apply conditional css on a child component's input field in Angular 5?

How to apply conditional attribute to HTML element in template?

Apply specific style to recursive angular element

Angular Parent/Child Directive's order of execution

Apply a particular style to all child elements of a Xamarin.Forms element

Apply a directive on a DOM element using getElementById in angular 7

Angular 2: Apply CSS Style with component selector

Listen to mouse event of a child element that the directive attached to - Angular 7

Angular2 protractor element directive

Conditional style inside an element

Conditional jQuery on element style

Apply directive from component to child

angular directive to style a checkbox