Get the name of a class in Angular?

1

I'm working with Angular 5 .

I am passing an object to a component, with the decorator @Input ():

@Input() x: any;

This object can be of class Xxx, Yyy or Zzz.

Can I know within the component if the object received is of one kind or another?

    
asked by Orici 21.05.2018 в 11:09
source

1 answer

1

If it really is a class, you can use instanceof :

let a= [];

console.log('¿Es un array?', a instanceof Array);

class Ejemplo {

}

a= new Ejemplo();

console.log('¿Es un Ejemplo?', a instanceof Ejemplo);
console.log('¿Es un array?', a instanceof Array);

This will not work if the declared type is interface or type , because they only exist at compile time (they are concepts from Typescript, it is not Javascript).

    
answered by 21.05.2018 в 11:58