6

I try to create a simple angular2 component, and I have an error when binding directive to a native DOM element.

For example:

/// <reference path="../../typings/_custom.d.ts" />

import { Component, View } from 'angular2/angular2';
import { RouterLink } from 'angular2/router';

@Component({
    selector: 'my-component',
    directives: [RouterLink]
})

@View({
    template: `
        <a [router-link]="['/page']">test</a>
    `
})

export class MyComponent {    }

=> Can't bind to 'routerLink' since it isn't a known property of the '<a>' element and there are no matching directives with a corresponding property.

What did I do wrong?

tzi
  • 8,719
  • 2
  • 25
  • 45

2 Answers2

11
  • As @EricMartinez said, "directives" is a "View" property
  • As @dSebastien said, "router-link" became "routerLink"
  • As @pardeep-jain said, "angular2/angular2" became "angular2/core", "View" annotation is being removed, no need of the typings line

Here is the right code:

import { Component } from 'angular2/core';
import { RouterLink } from 'angular2/router';

@Component({
    selector: 'my-component',
    directives: [RouterLink],
    template: `
        <a [routerLink]="['/page']">test</a>
    `
})

export class MyComponent {    }
gsamaras
  • 71,951
  • 46
  • 188
  • 305
tzi
  • 8,719
  • 2
  • 25
  • 45
1

As of Angular2 is in beta now so there are lot of changes has been made few of them here regarding this question may help to someone.

(in most cases there is no need to use view annotation because all the functionality of view annotation is gets included in Component annotation).

Community
  • 1
  • 1
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215