【问题标题】:What directives do in Angular 2?Angular 2 中有哪些指令?
【发布时间】:2016-05-18 09:47:14
【问题描述】:

我不明白这两个 @Directive 和指令之间的区别:[HeroAppMainComponent] 在下面这种情况下

 @Component({
      selector: 'hero-app',
      template: `
        <h1>Tour of Heroes</h1>
        <hero-app-main [hero]=hero></hero-app-main>`,
      styles: ['h1 { font-weight: normal; }'],
      directives: [HeroAppMainComponent]
    })

和..

import { Directive, ElementRef, Input } from '@angular/core';
@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }
}

【问题讨论】:

    标签: typescript angular angular2-directives


    【解决方案1】:

    简而言之,指令没有模板,而组件有。有几种指令类型:

    【讨论】:

      【解决方案2】:

      实际上指令在 Angular 2 中仍然存在。组件只是指令中最重要的类型,但不是唯一的。组件是带有模板的指令。但是您仍然可以编写没有模板的装饰器样式指令。 在这里,我们没有像 Angular 1 中那样的 .directive 函数,而是有简单的类,这些类被注释以赋予它们特定的行为。 导入组件的注解是:

      import {Component} from 'angular2/core';
      

      【讨论】:

        猜你喜欢
        • 2018-01-17
        • 2016-01-27
        • 2018-06-04
        • 1970-01-01
        • 2017-02-02
        • 2019-11-30
        • 2017-06-11
        • 2017-06-27
        • 1970-01-01
        相关资源
        最近更新 更多