Search code examples
angularshow-hide

show and hide angular 2


Having issue with showing boolean element. If i'm trying to set show hide element in html it works like:

<p class="post-more-text" (click)="model.isTextVisible = !model.isTextVisible;">{{model.moreOrLessValue}}</p>

If I set a function in component, the element isn't hiding

<p *ngIf="isTextVisible">
    {{model?.morePostText}}
</p>

<p class="post-more-text" (click)="onMoreInfoClick(model)">{{model.moreOrLessValue}}</p>

and component:

onMoreInfoClick(model) {
    model.isTextVisible = !model.isTextVisible;
}

What i'm doing wrong?


Solution

  • Using [hidden] attribute is not recommended for Angular2. Better use ngIf condition. in this way:

    <p *ngIf="model.isTextVisible">
        {{model?.morePostText}}
    </p>
    

    More you can read here


    edited: Additionaly, I think that you forget parent of your isTextVisible attribute. Should it look like: *ngIf="model.isTextVisible" ??