【问题标题】:Angular2 directives modifying its html hostAngular2 指令修改其 html 主机
【发布时间】:2016-04-01 07:27:02
【问题描述】:

我正在使用 Angular 2,我想要一个指令来修改其宿主 html 元素的行为。例如,我想要一个带有选择器 '[inner-directive]'InnerDirective,这样:

@Component({
selector: '[outer-component]'
template: `
<div
  inner-directive
></div>
`
})
class outerComponent{
someAttribute: boolean;
}

一样

@Component({
selector: '[outer-component]'
template: `
<div
  [ngClass] = "{'some-class': someAttribute}"
></div>
`
})
class outerComponent{
someAttribute: boolean;
}

我不希望 InnerDirective 成为一个组件,它有自己的模板来完成这项工作(someAttribute 被传递下来),因为这会创建一个多余的 html 元素。

更一般地说,我想通过将一个空组件放入另一个组件(具有上述 html 冗余)中,通过将内部组件改为指令来做任何可以实现的事情。这样我的“叶子”都可以是指令。

非常感谢您的帮助。

【问题讨论】:

    标签: angular angular2-template angular2-directives


    【解决方案1】:
    @Directive({
      selector: '[innerDirective]',
      host: {'[class.some-class]': 'innerDirective'}
    })
    class InnerDirective {
      @Input() innerDirective: boolean;
    }
    
    • host: {'[class.some-class]': 'someAttribute'}innerDirective 输入的值绑定到宿主元素上的some-class

    • 与选择器同名的输入 (@Input() innerDirective: boolean;) 允许使用简洁的绑定语法(如下所示)

    @Component({
      selector: '[outer-component]',
      directives: [InnerDirective],
      template: `
    <div [innerDirective]="someAttribute"></div>`
    })
    class outerComponent{
      someAttribute: boolean;
    }
    
    • [innerDirective]="someAttribute"someAttribute 值绑定到 InnerDirectiveinnerDirective 输入。

    【讨论】:

    • 非常感谢您的回答。快速跟进:简洁的绑定语法是什么意思?为什么是 host: {'[class.some-class]': 'someAttribute'} }) 而不是 host: {'[class.some-class]': 'innerDirective'} }) ?如果我需要多个输入,这可以工作吗?
    • 我所说的“简洁”的意思是您可以使用[innerDirective]="someAttribute" 而不是inner-directive [someAttribute]="someAttribute"。感谢您的提示,我已修复名称(someAttribute -> innerDirective
    【解决方案2】:

    这通常是属性指令的目标。它允许更改它所应用的元素的外观或行为。

    这是一个示例:

    @Directive({
      selector: '[inner-directive]'
    })
    export class InnerDirective {
      constructor(el: ElementRef) {
        el.nativeElement.style.backgroundColor = 'yellow';
      }
    }
    

    更新

    您可以在定义指令时利用host 属性

    @Directive({
      (...)
      host: {
        '[class.myClass]': 'displayClass',
        '[attr.someAttr]': 'someAttrValue',
        '[attr.otherAttr]': 'someValue'
      }
    })
    export class InnerDirective {
      @Input('innner-directive')
      someValue: string;
    
      constructor() {
        // myClass is added on the host
        this.displayClass = true;
    
        // myClass isn't added on the host
        // this.displayClass = false;
    
        this.someAttrValue = 'some value';
      }
    
      ngOnInit() {
        console.log(this.someValue);
      }
    }
    

    【讨论】:

    • 非常感谢您的回答。这真的可以适应将 dom 元素的属性(例如类、背景颜色等)绑定到某个表达式的值吗?构造函数不是只执行一次吗?
    • 是的,您可以使用host 配置属性绑定属性/类。在这种情况下,事情是动态的。我相应地更新了我的答案......
    • 你最好使用this.renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'yellow'); 在构造函数中注入渲染器:渲染器。我阅读了 WebWorker 安全。
    猜你喜欢
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多