简体   繁体   English

角度[隐藏]不起作用

[英]Angular [hidden] doesn't work

I have userPermission() function that works correctly, but component app-chat that depends on it always shows, even if userPermission() return false. 我有userPermission()函数,该函数可以正常工作,但是即使userPermission()返回false,也总是显示依赖于它的组件应用聊天。

home.ts file 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 file: home.html文件:

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

I also wrote 我也写了

[hidden] {
  display: none;
}

But this doesn't work. 但这是行不通的。

应该是这样

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

I think there could be a problem with the type of the response. 我认为回应的类型可能有问题。 Try to compare with == 'true' or === 'true' etc. I had such issues a time ago. 尝试与== 'true'=== 'true'等进行比较。我之前遇到过此类问题。

The problem could be: when 'false' is a string, it will return true (as string always returning true). 问题可能是:当'false'是字符串时,它将返回true (因为字符串始终返回true)。 In your case you are comparing to !true and that will be false . 在您的情况下,您将与!true进行比较,这将是false

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

Maybe I´m wrong, but it could be an issue. 也许我错了,但这可能是个问题。 Else provide an example in jsfiddle or similar. 另外,请使用jsfiddle或类似示例。

Update 更新资料

Try to do the asych on the params inside the function if this are your observables 如果可以观察到,请尝试对函数内部的参数进行异步处理

userPermission( (channel | asynch), (channelId | asynch)

Update2 更新2

Seem your vars are whether an Observable or return of an Promise you should use 似乎您的var是您应该使用的Observable还是Promise的返回

[hidden]="!userPermission(channel, channelId)"

without asynch. 不用急。 As you can see in your error message it is returning 'true' as wrong param for the pipe asynch. 正如您在错误消息中看到的那样,它为管道异步返回'true'作为错误的参数。

You want to apply an attribute conditionally. 您想有条件地应用属性。

You have to assign a value to make the attribute appear on the DOM element, and assign undefined to remove it from the DOM element. 您必须分配一个值以使该属性出现在DOM元素上,并分配undefined以将其从DOM元素中删除。

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

Personally, I don't like this approach. 我个人不喜欢这种方法。 It's easier to just use CSS classes. 仅使用CSS类会更容易。

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

CSS 的CSS

.hidden { display: none }

FYI : async is for observables and promises only. 仅供参考async仅适用于可观察和承诺。

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

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