Home > Back-end >  how to write this code in angular14 syntax?
how to write this code in angular14 syntax?

Time:12-26

this is the code: `

constructor(obj?: any) {
    this.id= obj && obj.id||null;
    this.title= obj && obj.title          || null;
    this.description= obj && obj.description    || null;
    this.thumbnailUrl= obj && obj.thumbnailUrl   || null;
    this.videoUrl= obj && obj.videoUrl       ||
                         `https://www.youtube.com/watch?v=${this.id}`;
  }

` it doesnt work in angular 14

i didnt try anything

CodePudding user response:

The first thing is you wont be using constructors like that. You should be using the constructor to inject services and providers. I'm pretty sure what your trying to accomplish can be done with ngOnInit and a method inside the class. If you need them to be assigned earlier than when ngOnInit, you can still put stuff in the constructor, just you cannot initialize them there. What you want to do is initialize the variables before the constructor.

CodePudding user response:

you can simply remove the obj &&

try as below

constructor(obj: any = {}) {
  this.id = obj.id || null;
  this.title = obj.title || null;
  this.description = obj.description || null;
  this.thumbnailUrl = obj.thumbnailUrl || null;
  this.videoUrl = obj.videoUrl || `https://www.youtube.com/watch?v=${this.id}`;
}
  • Related