博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Angular 2+中的ngShow和ngHide等效于什么?
阅读量:2378 次
发布时间:2019-05-10

本文共 3256 字,大约阅读时间需要 10 分钟。

本文翻译自:

I have a number of elements that I want to be visible under certain conditions. 在某些条件下,我希望看到一些元素。

In AngularJS I would write 在AngularJS中,我会写

stuff

How can I do this in Angular 2+? 如何在Angular 2+中做到这一点?


#1楼

参考:


#2楼

Just bind to the hidden property 只需绑定到hidden属性

[hidden]="!myVar"

See also 也可以看看

issues 问题

hidden has some issues though because it can conflict with CSS for the display property. hidden有一些问题,因为它可能与display属性的CSS冲突。

See how some in doesn't get hidden because it has a style 看看在中如何隐藏some样式,因为它具有样式

:host {display: block;}

set. 组。 (This might behave differently in other browsers - I tested with Chrome 50) (这在其他浏览器中可能会有所不同-我在Chrome 50上进行了测试)

workaround 解决方法

You can fix it by adding 您可以通过添加来修复它

[hidden] { display: none !important;}

To a global style in index.html . index.html的全局样式。

another pitfall 另一个陷阱

hidden="false"hidden="{
{false}}"hidden="{
{isHidden}}" // isHidden = false;

are the same as 与...相同

hidden="true"

and will not show the element. 并且不会显示该元素。

hidden="false" will assign the string "false" which is considered truthy. hidden="false"将分配被认为是真实的字符串"false"

Only the value false or removing the attribute will actually make the element visible. 实际上,只有值false或删除属性才会使该元素可见。

Using {

{}} also converts the expression to a string and won't work as expected. 使用{
{}}
还会将表达式转换为字符串,将无法正常工作。

Only binding with [] will work as expected because this false is assigned as false instead of "false" . 只有与[]绑定才能按预期工作,因为此false被分配为false而不是"false"

*ngIf vs [hidden] *ngIf[hidden]

*ngIf effectively removes its content from the DOM while [hidden] modifies the display property and only instructs the browser to not show the content but the DOM still contains it. *ngIf有效地从DOM中删除了它的内容,而[hidden]修改了display属性,仅指示浏览器不显示内容,但DOM仍然包含它。


#3楼

Use the [hidden] attribute: 使用[hidden]属性:

[hidden]="!myVar"

Or you can use *ngIf 或者您可以使用*ngIf

*ngIf="myVar"

These are two ways to show/hide an element. 这是显示/隐藏元素的两种方法。 The only difference is: *ngIf will remove the element from DOM while [hidden] will tell the browser to show/hide an element using CSS display property by keeping the element in DOM. 唯一的区别是: *ngIf将从DOM中删除该元素,而[hidden]则告诉浏览器通过将元素保留在DOM中来使用CSS display属性来显示/隐藏该元素。


#4楼

If your case is that the style is display none you can also use the ngStyle directive and modify the display directly, I did that for a bootstrap DropDown the UL on it is set to display none. 如果您的情况是样式不显示,您还可以使用ngStyle指令并直接修改显示,我这样做是为了将Bootdrop DropDown上的UL设置为不显示。

So I created a click event for "manually" toggling the UL to display 因此,我创建了一个点击事件,用于“手动”切换UL以显示

Then on the component I have showDropDown:bool attribute that I toggle every time, and based on int, set the displayDDL for the style as follows 然后在组件上,我具有showDropDown:bool属性,该属性每次都切换,并基于int,为样式设置displayDDL,如下所示

showDropDown:boolean;displayddl:string;manualtoggle(){    this.showDropDown = !this.showDropDown;    this.displayddl = this.showDropDown ? "inline" : "none";}

#5楼

Use hidden like you bind any model with control and specify css for it: 使用hidden就像您将任何模型与控件绑定并为其指定css一样:

HTML: HTML:

CSS: CSS:

[hidden] {   display: none;}

#6楼

myExpression可以设置为true或false

转载地址:http://cuexb.baihongyu.com/

你可能感兴趣的文章
高内聚 低耦合
查看>>
GUI开发之DirectFB
查看>>
GTK/DirectFB两个闪烁的问题
查看>>
《Linux内核修炼之道》 之 高效学习Linux驱动开发
查看>>
编写可移植C/C++程序的要点
查看>>
DirectFB代码导读
查看>>
linux fork函数浅析
查看>>
内核启动时间优化
查看>>
基于Linux的多播编程
查看>>
网络字节序
查看>>
Linux网络命令详解
查看>>
GNU C 的 __attribute__ 机制
查看>>
atoi,atol,strtod,strtol,strtoul详解
查看>>
基于HZK16的汉字显示技术
查看>>
嵌入式web服务器对比
查看>>
select 函数使用指难
查看>>
人类的15个欲望与游戏设计
查看>>
高速缓存
查看>>
kernel基本功练习
查看>>
UNIX/LINUX 平台可执行文件格式分析
查看>>