2016-05-14 29 views
3

是否可以分配到指令查看?Angular2指令分配給模板

import {Directive} from 'angular2/core'; 

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

而且在組件的視圖

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

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

我已經嘗試了許多其他的方式類似的結構指令(https://angular.io/docs/ts/latest/guide/structural-directives.html

總之:我的指令應該爲元素的新範圍(如角度1.x)

回答

4

這應該使用模板變量一樣

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

http://plnkr.co/edit/k8yEhA3ad106dMd0x9I9?p=preview 更新plnkr但沒有運氣 –

+0

好吧,我得到它的工作: ) –

+0

但是你的回答不是很準確 - 我必須在我的指令元數據中添加'exportAs:'foo'' –