Angular2:无法加载组件

时间:2016-03-17 04:48:34

标签: angular

我正在练习angular2,我正在尝试使用属性指令,但我无法加载组件而且我不知道我做错了什么

普兰克:http://plnkr.co/edit/o3llvK?p=preview

app.ts:

//our root app component
import {Component} from 'angular2/core'
import {AttributeDirective} from './attributedirective.component'

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <my-attr-directive></my-attr-directive>
    </div>
  `,
  directive: [AttributeDirective]
})
export class App {
  constructor() {
    this.name = 'Angular2'
  }
}

attributedirective.component.ts:

import {Component} from 'angular2/core'
import {HighlightDirective} from './highlight.directive'

@Component({
  selector: 'my-attr-directive',
  template: `
    <div myHighlight>Highlight me</div>
  `,
  directive: [HighlightDirective]
})
export class AttributeDirective{
  console.log("test"); 
}

highlight.directive.ts:

import {Directive, ElementRef, OnInit} from 'angular2/core'

@Directive({
  selector: '[myHighlight]'
})

export class HighlightDirective implements OnInit{
  private _defaultGreen = 'green';

  constructor (private _elRef: ElementRef){}

  ngOnInit():any{
    this._elRef.nativeElement.style.backgroundColor = this._defaultGreen;
  }
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

这会修复你的傻瓜...

export class AttributeDirective{
  consturctor(){
      console.log("test");   // you can't use console.log directly as you are working with typescript not javascript....
  }
}