Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind angular2 directive on native element

Tags:

angular

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?

like image 490
tzi Avatar asked Aug 27 '15 23:08

tzi


1 Answers

  • 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 {    }
like image 126
tzi Avatar answered Sep 24 '22 06:09

tzi