2016-09-21 81 views
1

我有一个奇怪的问题,我试图作为输入传递给我的指令布尔值。由于某种原因,角度是将布尔转换为字符串,尽管事实上我已将其键入为布尔值。角2(RC.6)指令@输入转换为字符串

组件,你可以看到背景是一个布尔值:

export class ModalsExportComponent extends Modal { 
    private background: boolean = false; 
    ... 
} 

模板,这里我结合背景,我的指令输入:

<label for='showBackground' cmgSharedCustomCheckbox='{{background}}'><span></span>Include Background</label> 

指令,在这里我定义输入和设置它的键入为布尔值,但它会以某种方式转换为字符串:

@Directive({ 
    selector: '[cmgSharedCustomCheckbox]' 
}) 

export class SharedCustomCheckboxDirective implements AfterViewChecked { 
    @Input('cmgSharedCustomCheckbox') isChecked: boolean; 

    constructor( private element: ElementRef, 
       private renderer: Renderer) { } 

    public ngAfterViewChecked(): void { 
    this.renderer.setElementClass(this.element.nativeElement.children[0], 'checkbox-unselected', true); 
    } 

    @HostListener('click') click(): void { 
    console.log(typeof this.isChecked); 
    if (this.isChecked) { 
     console.log('here'); 
     this.renderer.setElementClass(this.element.nativeElement.children[0], 'checkbox-unselected', false); 
     this.renderer.setElementClass(this.element.nativeElement.children[0], 'checkbox-selected', true); 
    } 
    } 
} 

您会注意到insi我的点击主机侦听器我有一个控制台日志的类型this.isChecked,这是日志字符串。我如何得到角度尊重事实,我已经告诉它,这个值是一个布尔值?

回答

1

如上所述here

材料中的大括号之间是一个模板表达式角首先评估,然后转换为字符串。

要绑定非字符串值,可以使用

<label for='showBackground' [cmgSharedCustomCheckbox]='background'>...</label>