程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量?

开发过程中遇到我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量的问题如何解决?下面主要结合日常开发的经验,给出你关于我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量的解决方法建议,希望对你解决我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量有所启发或帮助;

Country.component.ts

@H_618_7@ countryList :any;
  searchvalue : String = "" ;

  constructor( private service : SharedservicE) { }
  
  ngOnInit(): voID {
    this.refreshCountryList();
  }
  
  refreshCountryList(){
    this.service.getCountryList().subscribe(data=>{ this.countryList = data 
      console.log("countryList insIDe subscribe :",this.countryList ) ;
    });
    console.log( "List outsIDe subscribe :",this.countryList);
  }

shared.service.ts

@H_618_7@export class Sharedservice {

  Readonly APIUrl = "https://localhost:44383/API/CountryAPI/";

  constructor(private http: httpClIEnt) { }
  
  getCountryList(){
    return this.http.get(this.APIUrl + 'Listcountry');
  }


}

我无法在订阅外获取 countryList 值,范围仅在订阅内。

如何获取外部订阅的价值?

解决方法

Angular 同步执行代码。 @H_618_7@subscribe() 实际上是在所有同步代码执行后最后执行的。所以这一行在调用 subscribe 之前首先执行。

@H_618_7@console.log( "list outside subscribe :",this.countryList);

但是,如果您只想查看输出,您可以在 HTML 端放置一个 JSON 管道并在视图中查看。

@H_618_7@<pre> <div [innerHTML]="this.countryList | json"></div> >/pre> 

另一种方法是存储数据并调用一个方法来检查数据是否可用,进而可以用于将逻辑放入其中。

@H_618_7@countryList :any;
searchvalue : String = "" ;

constructor( private service : SharedservicE) { }
  
ngOnInit(): void {
  this.refreshCountryList();
}
  
maPDAta(){
  console.log(this.countryList);
}

refreshCountryList(){
  this.service.getCountryList().subscribe((data)=>{
    if(!data) return;
    this.countryList = data;
    console.log("countryList inside subscribe :",this.countryList ) ;
    this.maPDAta();
  });
  console.log( "list outside subscribe :",this.countryList);
}

大佬总结

以上是大佬教程为你收集整理的我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量全部内容,希望文章能够帮你解决我正在尝试实现 refreshCountryList ,它应该返回包含所有国家/地区的 countryList(array of objects) 变量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。