Arūnas Smaliukas
Arūnas Smaliukas

Reputation: 3321

Angular2 directive assign to template

Is it possible to assign to view from directive?

import {Directive} from 'angular2/core';

@Directive({
  selector: '[foo]'
})
export class FooDirective {
   this.bar:string = "baz";
}

And in component's view

<div foo>
 bar value should be 'baz': {{bar}}.
</div>

http://plnkr.co/edit/To6hj9CJi1caz2pSF0RP?p=preview

I have tried many other ways like structural directives (https://angular.io/docs/ts/latest/guide/structural-directives.html)

In short: my directive should create new scope for element (like in angular 1.x)

Upvotes: 3

Views: 349

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657466

This should work using a template variable like

@Directive({
  selector: '[foo]',
  exportAs: 'foo`
})
export class FooDirective {
   this.bar:string = "baz";
}
<div foo #foo="foo">
 bar value should be 'baz': {{foo.bar}}.
</div>

Upvotes: 4

Related Questions