Angular 2 - 将条件样式应用于指令的子HTML元素

时间:2016-01-24 17:53:23

标签: angular angular2-template angular2-directives

我正在尝试基于click事件将类应用于HTML元素。从父组件的模板设置子组件的选择器的类属性时,这可以正常工作,如下面的父组件片段所示:

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

这将在单击该项目时适当设置样式。但是,我想基于相同类型的单击事件在子组件中设置内部HTML元素的类,这里是子组件样式的所需目标:

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

有没有办法做到这一点,很容易支持?或者这被认为是一种不好的做法,我应该设计我的组件以避免这种条件样式的情况?

完整代码:

@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 {}

3 个答案:

答案 0 :(得分:4)

child-component

添加样式
@Component({
  selector: 'child-component',
  inputs: ['item'],
  template: `
    <div class="This is where I really want to apply the style">  
      {{ item.val }}
    </div>
  `,
  styles: [`
    :host(.bordered) > div {
    // if this selector doesn't work use instead
    // child-component.bordered > div {
      border: 3px solid red;
    }
  `],
})
export class ChildComponent {}

答案 1 :(得分:4)

我找到了一种更好的方法来解决这个问题,充分利用了Angular2的功能。

具体而言,您可以通过更改以下内容而不是使用:host和CSS功能进行欺骗,而只需将变量传递给子组件:

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

在子类的元素中设置,将其更改为

[isBordered]='isSelected(item)'

然后在div上你想将边界类应用到子组件的模板中,只需添加:

[ngClass]='{bordered: isBordered}'

以下是更改的完整代码:

@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 {}

答案 2 :(得分:0)

这样的事情对我来说非常好:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  styleUrls: [ './app.component.css' ],
  template: `
  <button 
    (click)='buttonClick1()' 
    [disabled] = "btnDisabled"
    [ngStyle]="{'color': (btnDisabled)? 'gray': 'black'}">
    {{btnText}}
  </button>`
})
export class AppComponent  {
  name = 'Angular';
  btnText = 'Click me';
  btnDisabled = false;
  buttonClick1() {
    this.btnDisabled = true;
    this.btnText = 'you clicked me';
    setTimeout(() => {
      this.btnText = 'click me again';
      this.btnDisabled = false
      }, 5000);
  }
}

这是一个可行的示例:
https://stackblitz.com/edit/example-conditional-disable-button?file=src%2Fapp%2Fapp.component.html