0

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?

2
  • 1
    The property is called directives, not directive. You missed the s. Here's your plnkr working. Commented Mar 17, 2016 at 4:56
  • Thank you!! I noticed it and corrected it after posting my question here. However, my plunk didn't work. Commented Mar 17, 2016 at 4:58

1 Answer 1

2

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....
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.