What is the equivalent of ngShow and ngHide in Angular 2+?

后端 未结 18 2455
离开以前
离开以前 2020-11-22 14:46

I have a number of elements that I want to be visible under certain conditions.

In AngularJS I would write

stuff
18条回答
  •  我在风中等你
    2020-11-22 15:24

    According to Angular 1 documentation of ngShow and ngHide, both of these directive adds the css style display: none !important;, to the element according to the condition of that directive (for ngShow adds the css on false value, and for ngHide adds the css for true value).

    We can achieve this behavior using Angular 2 directive ngClass:

    /* style.css */
    .hide 
    {
        display: none !important;
    }
    
    
    
    I'm Angular1 ngShow...
    I'm Angular2 ngShow...
    I'm Angular1 ngHide...
    I'm Angular2 ngHide...

    Notice that for show behavior in Angular2 we need to add ! (not) before the ngShowVal, and for hide behavior in Angular2 we don't need to add ! (not) before the ngHideVal.

提交回复
热议问题