0

I want to know from where my data is coming from which class, from which method
let's take an example like

EditValue(data)
{
  /* my operations */
}

Now there are multiple classes in my project which send the data to this EditValue(data) method.
Is there any way in Typescript or in angular to know from which class that data is coming or from which class method that data is coming.
like className.method somewhat like this

3 Answers3

2

If you want to know from try using JSON.stringify(console.trace())

I am sure it will store your data in variable

chokiChoki
  • 164
  • 13
0

You can add it in the data that gets passed to the method itself. Assign it when making the call and get the details using data['yourTagName'] in EditValue

  • I don't want to that thing. I don't want my developers to write a class name or method name and send the data to my editValue method – Dexter's Web Lab Sep 28 '18 at 11:04
0

Here is simple example, hope it will help you

export class AppComponent implements OnInit {
  user: User = new User();
  constructor() {

  }
  ngOnInit(): void {
    this.user.FirstName = "Ali Shahbaz";
    this.EditValue(this.user);
  }

  EditValue(data: any) {
    console.log(Object.getPrototypeOf(data));
  }
}

export class User {
  public FirstName: string;
  constructor(){
    this.FirstName = "";
  }
}

and response is

enter image description here

Ali Shahbaz
  • 825
  • 1
  • 7
  • 19
  • it's not what I want, it's giving me the class name but I tried to retrieve it but it didn't work – Dexter's Web Lab Sep 28 '18 at 12:01
  • This is the way you can access class name, `Object.getPrototypeOf(data).constructor.name` but for function you need call stack, but it's not standard and not recommended https://stackoverflow.com/a/280396/1203961 – Ali Shahbaz Sep 28 '18 at 12:12
  • bro it's giving me: Object instead it should be CurdTableComponent. I am using it in service – Dexter's Web Lab Sep 28 '18 at 12:17
  • will have a glance on that link you provided – Dexter's Web Lab Sep 28 '18 at 12:18
  • I just went through the link that you provided and I got the error which is mention in the screenshot you provided in argument and caller – Dexter's Web Lab Sep 29 '18 at 07:33
  • Yes, in my comments provided URL, they are clearly saying caller works in non strict environment. There's another way to check call stack but I don't think that will work for you. `new Error().stack` – Ali Shahbaz Sep 29 '18 at 09:08
  • wil try this ,if still it wnt work then try to thnk to apply through another way or process, thanks fr ur efforts bro – Dexter's Web Lab Sep 29 '18 at 09:25