简体   繁体   English

Angular2指令分配给模板

[英]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 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 ) 我尝试了许多其他方法,如结构指令( 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) 简而言之:我的指令应该为元素创建新的范围(比如在角度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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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