简体   繁体   中英

Angular2: Unable to load components

I am practicing angular2 and I'm trying to work with attribute directives but I'm unable to load components and I have no clue what I'm doing wrong

Plunk: 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;
  }
}

Could someone please help me fix this?

this will fix your plunker...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM