简体   繁体   English

如何在 angular 中传递 Javascript 变量(JSON 数据)以加载表中的动态数据

[英]How to pass Javascript variable (JSON Data) in angular for loading dynamic data in the table

How to pass Javascript variable in angular, this.http.get(jsvariable) for loading dynamic data in the table如何在angular、this.http.get(jsvariable)中传递Javascript变量来加载表中的动态数据

public jsondata = [
      { id: 860, firstName: "Superman", lastName: "Yoda" },
      { id: 861, firstName: "xyz", lastName: "Abc" }
    ];

Need to call js variable需要调用js变量

 this.http.get(this.jsondata)
    .subscribe(res => {
      this.results3 = res as any;
    });

Stackblitz 堆栈闪电战

You dont need to do a http call because the json is inside of you code, I mean you have the reference of thisobject to used directly你不需要做一个 http 调用,因为 json 在你的代码里面,我的意思是你有这个对象的引用可以直接使用

only wrap the jeson into a function, and call this function whenever you want仅将 jeson 包装到 function 中,并在需要时调用此 function

getJsonData(){
 return [
      { id: 860, firstName: "Superman", lastName: "Yoda" },
      { id: 861, firstName: "xyz", lastName: "Abc" }
    ];
}
// OR The same function simulating you are getting an observable
// you need to import 'of' from rxjs
getJsonData(){
 return of([
      { id: 860, firstName: "Superman", lastName: "Yoda" },
      { id: 861, firstName: "xyz", lastName: "Abc" }
    ]);
}

The http call its used when you are going to do a http request to some server or some API provider.当您要向某些服务器或某些 API 提供程序发出 http 请求时,将使用http调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM