10

I have one component with one html and .ts file. For some reasons I have to create multiple pages (HTML) pages.
Is it possible to create multiple (html) pages for single class (component)?
Or is it possible to provide dynamic TemplateUrl to the component?

My main motive is to provide different URL and to use different view (HTML pages) but using single class (.ts class i.e component)?
I have seen many questions referred below related to that but unable to found exact solution-

I want something like this

{ path: '/Login', component: LogIn, name: "Login"},
{ path: '/Login2', component: LogIn, name: "Login" },
{ path: '/Login3', component: LogIn, name: "Login" }

I want to load same component with different url and view.

9
  • 1
    So what's the problem with the solutions provided in the linked SO questions? Commented May 13, 2016 at 8:41
  • none of them is working for me. Commented May 13, 2016 at 8:42
  • How are they not working for you? Commented May 13, 2016 at 8:45
  • 1
    any alternate than without *ngIf ? exactly i want diff. templateUrls for diff. routes Commented May 13, 2016 at 8:50
  • 2
    I just wanted to explicitly mention again that if you have a component that extends a class that all decorators @Component(), @Input(), @Output(), @ViewChile(), ... need to be on the class that is actually used as component. If they are only on a subclass they are ignored (mostly) Commented May 13, 2016 at 9:38

2 Answers 2

7

This is possible with inheritance. Different view and style templates, but (basically) the same underlying component code. You still have to declare the @Component metadata, so if you're using value accessors etc, you will need to redefine those too.

First component:

import { Component, Input, OnInit } from '@angular/core';

import { KeyValuePairBase } from '../../model/key-value-pair-base'

@Component({
  moduleId: module.id,
  selector: 'select-list',
  templateUrl: './select-list.component.html',
  styleUrls: ['./select-list.component.scss']
})
export class SelectListComponent implements OnInit {
  @Input() private data: KeyValuePairBase[];

  constructor() {
    super();
  }

  ngOnInit() {
    if (!this.name) throw new Error("'name' property is required by 'select-list' component");
    if (!this.friendlyName) throw new Error(`'friendlyName' property is required by 'select-list' component '${this.name}'`);
  }
}

Second component:

import { Component } from '@angular/core';

import { SelectListComponent } from '../select-list/select-list.component'

@Component({
  moduleId: module.id,
  selector: 'radio-list',
  templateUrl: './radio-list.component.html',
  styleUrls: ['./radio-list.component.scss']
})
export class RadioListComponent extends SelectListComponent {
}
Sign up to request clarification or add additional context in comments.

Comments

2

Angular best practice is one component includes one template. If you want to apply the same logic to two views, you should create that logic inside a service, and use one service for two different component-view sets. URLs are a different issue, handled by the router. You should assign component A to URL A and component B to URL B.

1 Comment

he is asking for creating two blank components with logic in single service class. You will need two different URLs for this. @PardeepJain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.