简体   繁体   English

ngIf和@Input angular2奇怪的行为

[英]ngIf and @Input angular2 odd behavior

NgIf seems to always be true even though the console says false. NgIf似乎始终为true,即使控制台显示为false。

From the following component html 来自以下组件html

 <product-component-tree itemSku="{{item.itemSku}}" selectable="false" classes="col-md-12 col-xs-12"></product-component-tree>

which sets selectable to false... 将选择设置为假...

export class ProductComponentTree {
@Input() classes: string;
@Input() itemSku: string;
@Input() selectable: boolean;
ngOnInit() {
    if (this.itemSku)
        this.productDetailService.getComponents(this.itemSku, true).subscribe(x => {
            this.treeData = x;
        });
    console.log(this.selectable); //prints false
}
}

Html tmplate for the component: 组件的HTML tmplate:

<div class="{{classes}}" *ngIf="selectable">
    something
    <p-tree [value]="treeData" layout="horizontal" selectionMode="checkbox" [(selection)]="selectedProducts" (onNodeSelect)="nodeSelect($event)"></p-tree>
</div>
<div class="{{classes}}" *ngIf="!selectable">
    else
    <p-tree [value]="treeData" layout="horizontal" [contextMenu]="productTreeCm" selectionMode="single" [(selection)]="selectedProduct"></p-tree>
    <p-contextMenu #productTreeCm [model]="items"></p-contextMenu>
</div>

always shows the div with something in it! 总是显示div中有东西!

Goal: Have it work properly and show else div if selectable is false. 目标:使其正常工作,如果selectable为false,则显示else div。

when you do selectable="false" , you pass a string value to the component. 当您执行selectable="false" ,您将字符串值传递给组件。

And, as you know "false" is a truthy value. 而且,如您所知,“ false”是真实值。 (otherwise, have a look at this : https://dorey.github.io/JavaScript-Equality-Table/ ) (否则,请看一下: https : //dorey.github.io/JavaScript-Equality-Table/

What you have to do is the following : 您需要执行以下操作:

<product-component-tree [itemSku]="item.itemSku" [selectable]="false" ...

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

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