简体   繁体   中英

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)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM