What is the equivalent of ngShow and ngHide in Angular?
What is the equivalent of ngShow and ngHide in Angular?
In Angular, the equivalents of the AngularJS directives ngShow
and ngHide
are primarily handled by the [hidden]
attribute and the *ngIf
structural directive.
[hidden] Attribute: This attribute is used to control the visibility of elements in Angular. It works by setting the CSS display
property of the element. If the expression assigned to [hidden]
evaluates to true
, the element is hidden; if false
, the element is displayed. However, it's important to note that the [hidden]
attribute does not remove the element from the DOM; it merely changes its display style to none
[1][2][5].
Example:
<div [hidden]="!isVisible">This content is conditionally visible</div>
*ngIf Directive: Unlike the [hidden]
attribute, *ngIf
actually adds or removes elements from the DOM based on the truthiness of the expression provided. If the expression evaluates to true
, the element is added to the DOM; if false
, the element is removed. This approach is more performance-efficient when the visibility of an element changes frequently, as it re...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào