how to best conditional template show in angular 4
Asked Answered
O

4

18

currently, i am practiced angular 4. when a normal user view this then show public content When A Registered user enter the web page then show edit or some content. how to the best practices show conditionally template Or Some Html Contents Example:

<div *ngIf="isUser">
    some Content  here......
</div>
<div *ngIf="!isUser">
    some Content here .....
</div>

actually, i want to know how to the best.

Ought answered 16/10, 2017 at 12:11 Comment(1)
Possible duplicate of Angular 2- using *ngIf with multiple conditionsRoss
U
49

In angular 4 you can use if.. else.. structure for html templates

You can use it in this way:

<div *ngIf="isUser; else otherContent">
    some Content  here......
</div>
<ng-template #otherContent>
    <div>
        some Content here .....
    </div>
</ng-template>

but in your case, the prettiest solutions will be if... then... else... conditional

<ng-container *ngIf="isUser; then someContent else otherContent"></ng-container>

<ng-template #someContent ><div>some content...</div></ng-template>
<ng-template #otherContent ><div>other content...</div></ng-template>
Undefined answered 16/10, 2017 at 12:13 Comment(0)
F
6

NgIf is a structural directive. That means your component would be destroyed when the expression becomes false.

So if your component is often destroyed and created again than this can be an issue. In this case the [hidden] directive should be considered. It only sets display:none. In this case your component would not be destroyed.

<div [hidden]="expression">{{val}}</div>
Frendel answered 16/10, 2017 at 12:40 Comment(0)
S
2

You can change the template and style files at the compilation level as well. We use different templates and styles due to environment variables. For large projects, this approach happened to be more clear for us.

First, we add two fields to the environment.cs files like:

export const environment = {
   production: false,

   componentTemplateFile: './tenants/tenant-name.html',
   componentStyleFile: './tenants/tenant-name.scss',

   ...
}

Then, while declaring the components instead of using inline strings we use those environment variables like:

@Component({
  selector: 'app-enroll-footer',
  templateUrl: environment.componentTemplateFile,
  styleUrls: [environment.componentStyleFile]
}) 
export class EnrollFooterComponent implements OnInit, OnDestroy {
...

Lastly, instead of adding files to the root of the component folder, we have a child folder named tenants, and the template and style files are located under that with the names declared in the environment.cs files.

Example folder structure:

enroll-footer/
enroll-footer/enroll-footer.component.ts
enroll-footer/tenants/tenant-name.html
enroll-footer/tenants/tenant-name.scss
enroll-footer/tenants/other-tenant-name.html
enroll-footer/tenants/other-tenant-name.scss

When we change the environment variables (for another configuration) to other-tenant-name, the angular cli basically uses these files while compiling.

Samaritan answered 3/5, 2022 at 9:45 Comment(0)
A
1

use NgIf with else condition

<div *ngIf="isUser;else Other">
    some Content  here......
</div>

<ng-template #Other> some Content here .....</ng-template>
Astringent answered 16/10, 2017 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.