Angular Nativescript可见性默认为true,即使在初始化变量时也是如此
有没有办法在Nativescript中默认“隐藏”元素 我的组件中有Angular Nativescript可见性默认为true,即使在初始化变量时也是如此,angular,nativescript,angular2-nativescript,Angular,Nativescript,Angular2 Nativescript,有没有办法在Nativescript中默认“隐藏”元素 我的组件中有 export class HouseholdContactComponent{ private isLoading = true } 在我的xml中 <StackLayout [visibility]="isLoading ? 'collapse' : 'visible'"> <Label text="Hi there"></Label> </StackLayout>
export class HouseholdContactComponent{
private isLoading = true
}
在我的xml中
<StackLayout [visibility]="isLoading ? 'collapse' : 'visible'">
<Label text="Hi there"></Label>
</StackLayout>
使用此代码,屏幕会闪烁“Hi there”(你好)消息,然后它就会消失。您应该使用Angular
*ngIf
指令,而不是使用[visibility]属性
另一方面,为了绑定到属性属性并将其绑定到属性,您需要在其前面加上attr
,例如,您需要编写[attr.visibility]
,而不仅仅是[visibility]
。如果变量值为真
,则会呈现该属性
例:
//HTML
//组成部分
...
onTap(){
如果(this.isVisible){
this.isVisible=false;
}否则{
this.isVisible=true;
}
}
...
为什么不使用ngIf
?我使用了您共享的代码并解决了我的问题。谢谢你的提问;-)
//HTML
<Button text="Show/hide block" (tap)="onTap()" class="btn btn-primary btn-active"></Button>
<GridLayout *ngIf="isVisible" class="bg-primary" borderRadius="2" height="300"></GridLayout>
//Component
...
onTap() {
if (this.isVisible) {
this.isVisible = false;
} else {
this.isVisible = true;
}
}
...