2016-06-07 79 views
1

设置输入类型,我有以下指令:Angular2:在指令

@Directive({ 
    selector: "[myDir]" 
}) 
export class MyDir implements OnInit { 
    private el:HTMLElement 

    @Input() hide: boolean 


    constructor(el: ElementRef) { 
     this.el = el.nativeElement 
    } 

    ngInit() { 
     console.log(typeof this.hide) // Writes string in the console 
    } 
} 

模板:

<p myDirnumber=1 hide=false></p> 

反正是有,我可以迫使隐藏采取boolean类型?

回答

3

这可能会做你想做的。

<p myDirnumber=1 [hide]="false"> 

没有[] Angular将值解释为字符串而不是表达式。

+2

现货,谢谢!我没有忘记这一点。 – Scipion