Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 523
Arūnas Smaliukas Avatar asked Jun 07 '26 04:06

Arūnas Smaliukas


1 Answers

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>
like image 125
Günter Zöchbauer Avatar answered Jun 08 '26 18:06

Günter Zöchbauer