I am new to angular 2, I have a component named Register, in this 1 component I have 5 HTML pages where one click of 1st register page I will go to the 2nd register page and on click of 2nd register page I will go to the 3rd register page. How can I make 5 HTML pages in 1 component I means is there a way to achieve multiple templates per component? How to do routing? The main intent is to have separate HTML & SCSS files and routing logic.
As of now I am rendering pages using ngIf
which has made my page very lengthy. Is there a way around to achieve this?
<!--View 1-->
<div class="open-card-BG" *ngIf="registerView=='regView1'">
Register Page 1
</div>
<!--View 2-->
<div class="open-card-BG" *ngIf="registrationView=='regView2'">
Register Page 2
</div>
@Component({
selector: 'register-page',
templateUrl: './register-page.component.html',
styleUrls: ['./register-page.component.scss'],
providers : [RegisterService,Configuration,LocalStorageService]
})
ngOnInit() {
this.registerView= "regView1";
}
changeView(view) {
this.registerView= view;
}
previousView(view) {
this.registerView= view;
}
A component is composed of two Files Ts and HTML mainly. Html is the view. but you can not have multiple templates, as binding will be issue and rendering can not be done. Instead, you can have multiple components inside one component, with each having different templates you need.
You can simply extend your base component and overwrite the template. This allows you to have different components with the exact same functionality, but different templates. Save this answer.
There are several ways to add styles to a component: By setting styles or styleUrls metadata. Inline in the template HTML. With CSS imports.
Try do like this :
@Component({
selector: 'register-page',
template: `
<div class="open-card-BG" *ngIf="registerView == 'regView1'">Reg View 1 Content</div>
<div class="open-card-BG" *ngIf="registerView == 'regView2'">Reg View 2 Content</div>
`,
styleUrls: ['./register-page.component.scss'],
providers: [RegisterService, Configuration, LocalStorageService]
})
export class Appcomponent {
registerView = 'regView1';
}
Else do like this
page1.component.html
<div>
<h1>Page1 Component Content</h1>
</div>
page2.component.html
<div>
<h1>Page2 Component Content</h1>
</div>
home.component.html
<div>
<div class="open-card-BG" *ngIf="registerView == 'regView1'">
<app-page1-component></app-page1-component>
</div>
<div class="open-card-BG" *ngIf="registerView == 'regView2'">
<app-page2-component></app-page2-component>
</div>
</div>
component.ts
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
registerView = 'regView1';
}
Using *ngIf would be the most sensible way to do this. If it gets to the point that you are having to use *ngIf to cover large chunks of HTML then it's probably more of an indication that these should be separate components since they clearly have significantly different views.
If there is a lot of shared logic in your .ts files you can make a class with all the shared logic and use class inheritance on your individual components.
export class BaseComponentLogic implements OnInit {
...
}
@Component({...})
export class MyFirstComponent extends BaseComponentLogic implements OnInit {
...
}
@Component({...})
export class MySecondComponent extends BaseComponentLogic implements OnInit {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With