Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度[隐藏]不';行不通_Javascript_Angular - Fatal编程技术网

Javascript 角度[隐藏]不';行不通

Javascript 角度[隐藏]不';行不通,javascript,angular,Javascript,Angular,我有一个userPermission()函数可以正常工作,但依赖于它的组件app chat总是显示出来,即使userPermission()返回false home.ts文件 async userPermission(channel, channelId) { if (channel === undefined) { return false; } else if (channelId && channel.status === 'open') {

我有一个userPermission()函数可以正常工作,但依赖于它的组件app chat总是显示出来,即使userPermission()返回false

home.ts文件

async userPermission(channel, channelId) {
    if (channel === undefined) {
        return false;
    } else if (channelId && channel.status === 'open') {
        return true;
    } else if (channelId && channel.status === 'private' && await this.inChannel(channelId)) {
        console.log('private have permission');
        return true;
    } else {
        return false;
    }
}
home.html文件:

<app-chat [hidden]="!userPermission(channel, channelId) | async" [channelId]="channelId" [channel]="channel"></app-chat>
但这不起作用。

应该是这样的

<app-chat [hidden]="!userPermission(channel, channelId | async )" [channelId]="channelId" [channel]="channel"></app-chat>

我认为响应类型可能有问题。尝试与
==“true”
==“true”
等进行比较。我很久以前就遇到过这样的问题

问题可能是:当“false”是字符串时,它将返回
true
(因为字符串总是返回true)。在您的情况下,您正在与
进行比较!true
,这将是
false

<app-chat [hidden]="userPermission(channel, channelId) != true | async" [channelId]="channelId" [channel]="channel"></app-chat>
更新2

似乎你的VAR是你应该使用的承诺的可观察或回报

[隐藏]=“!userPermission(频道,频道ID)”


没有异步。正如您在错误消息中看到的,它返回的
'true'
是管道异步的错误参数

您希望有条件地应用属性

必须指定一个值以使属性显示在DOM元素上,并指定
undefined
以将其从DOM元素中删除

<app-chat [attr.hidden]="!userPermission(channel, channelId) ? true : undefined" [channelId]="channelId" [channel]="channel"></app-chat>

FYI
async
仅用于可观察和承诺。

您尝试过ngClass吗?我尝试过,但结果相同。尝试
[隐藏]=“!(用户权限(通道,通道ID)| async)”
。或者反转方法结果并使用:
[hidden]=“userNotAllowed(channel,channelId)| async”
。感谢您的回答,但它不起作用。现在,它返回管道“AsyncPipe”的
错误错误:InvalidPipeArgument:'-LEAgSiXK-FL7b_ggO1Z'
,其中
-LEAgSiXK-FL7b_ggO1Z
是通道IDThank,但它返回
错误:InvalidPipeArgument:'true'(管道“AsyncPipe”
@СааааСаааааtrue“?@БааСаааааааааааааа1072?Codepen或你喜欢的东西…如果你构建你的应用程序,它能工作吗?我的意思是,你能看到你在实时源代码中所做的更改吗?谢谢,但不幸的是,它也不起作用。应用聊天组件始终处于加载状态。您没有要求它不要加载。你要求不要展示。改用ngIf,阅读文档中的“入门”教程。对不起,我的意思是它无论如何都会显示出来。我试过ngIf,结果也一样。
userPermission( (channel | asynch), (channelId | asynch)
<app-chat [attr.hidden]="!userPermission(channel, channelId) ? true : undefined" [channelId]="channelId" [channel]="channel"></app-chat>
<app-chat [class.hidden]="!userPermission(channel, channelId)" [channelId]="channelId" [channel]="channel"></app-chat>
.hidden { display: none }