将数据从函数内部的变量传递到声明的变量

时间:2019-01-11 08:54:07

标签: javascript angular function typescript ionic-framework

我是新来的开发人员,需要这个问题的帮助,我希望不仅在函数内部,全局变量getCountry()内的变量regionFiltered的数据

      export class ShipmentManagementRegionPage {
      countryRegion;
      region;
      mainRegion;
      allRegions;
      filteredRegions;
      regionsFiltered;

      constructor(public navCtrl: NavController, public events: Events, public commerceFunction: CommerceFunction, public navParams: NavParams, public storage: Storage, ) {
      console.log(resgionsFiltered)
      }

      getCountry() {

          this.commerceFunction.Getadm1Shipping().subscribe(data => {
            this.mainRegion = data;
            console.log(this.mainRegion)
            this.storage.get("countryRegionList").then((data) => {
              this.countryRegion = data;
              console.log(this.countryRegion)
            })
            this.commerceFunction.GetGeoData_adm1OfCountry().subscribe(data => {
              this.region = data;

              for (let index = 0; index < this.mainRegion.length; index++) {
                let commonCountryRegion = this.countryRegion.find(x => x.CountryRegionCode == this.mainRegion[index].CountryRegionCodeID);
                this.mainRegion[index].CountryRegion = commonCountryRegion
                let commonAdm1 = this.region.find(x => x.cc_fips == this.mainRegion[index].CountryRegionCodeID);
                this.mainRegion[index].CountryRegion.adm1Shipping = commonAdm1
                let commonRegions = this.region.filter(x => x.cc_fips == this.mainRegion[index].CountryRegionCodeID);
                this.mainRegion[index].CountryRegion.geodata_adm1 = commonRegions
                this.regionsFiltered = commonRegions;
              }
              this.rowData = this.mainRegion

            });

          })

1 个答案:

答案 0 :(得分:0)

由于这是打字稿,因此您必须在窗口对象上声明regionFiltered属性:

declare global {
    interface Window { regionsFiltered: any; }
}

然后您可以设置它:

for (let index = 0; index < this.mainRegion.length; index++) {
    ...
    this.regionsFiltered = commonRegions;
    window.regionsFiltered = this.regionsFiltered;
}

那么无论您身在何处,都可以使用window.regionsFiltered