-1

Is it a valid typescript / Angular, or correct to assign a component to a variable? Just assigning , without type, etc.

For example:

... 
import {CompB} from './comp-b.component';

export class CompA {
   let c = CompB; 

}

Or the right way will be to create an interface / use generic , etc?

Thanks.

Menahem Gil
  • 787
  • 2
  • 15
  • 39
  • The Typescript compiler already tells you whether it's valid Typescript. Being able to start the app tells you if Angular likes it. What's the real question here? – Ingo Bürk Jul 11 '18 at 04:31
  • Hi, see bellow in my comment. Need to use it in Angular material. The CLI "likes" it (doesn't care). But was wonder if it's really Ok / valid, or I should create a type / interface – Menahem Gil Jul 11 '18 at 04:36
  • You should ask your question in the question, not in the comments of answers. You can still edit the question. – Ingo Bürk Jul 11 '18 at 05:25
  • Ok, thanks. I just posted a new question - this time focus on this issue. The reason I first asked this one, because it could solve the issue, as well, it's a general question – Menahem Gil Jul 11 '18 at 05:44

2 Answers2

0

I think you wan to communicate between components. If this is what you want then you should communicate using:

  1. Property Binding with Input and Output decorators
  2. A common service can be created to communicate and pass the data through this service.

any of the ways will work.

It's not good to use 1 component into another.

ngWolf
  • 307
  • 1
  • 6
  • 21
0

You create Service/Interface to achieve same functionality you want. Using component into another component is not a good idea.

You can communicate using service. For more information follow this URL: https://hackernoon.com/creating-interfaces-for-angular-services-1bb41fbbe47c

Sateesh
  • 1,327
  • 9
  • 12
  • Hi, thanks. It's not to communicate between components. It's to use with angular material. in order to use the .open() method, I need to open a modal with different components conditionally. The thing is that the .open() method apparently not accepts string as parameter , only the component name. Mean, if I put the component name in a string, then as a parameter to .open() , it will not accept it and gives error. Therefore need to assign the component to a variable, so .open() will accept it. Instead of .open(CompB, ) , .open(c, ) – Menahem Gil Jul 11 '18 at 04:27
  • Okay, once check this post. Its also related to open dialog with angular material. https://stackoverflow.com/questions/48073057/angular-open-close-sidenav-from-another-component – Sateesh Jul 11 '18 at 04:47