【问题标题】:Unable to filter json data for country,state,city dropdown无法过滤国家、州、城市下拉列表的 json 数据
【发布时间】:2019-01-24 12:02:26
【问题描述】:

我正在开发一个国家、州、城市可靠的下拉列表,我需要根据国家/地区填充州详细信息,但我无法过滤它。谁能帮我解决这个问题。我收到以下错误

“订阅”类型上不存在属性“过滤器”。

     onSelectCountry(country_id: number) {
        this.selectedCountry = country_id;
        this.selectedState = 0;
        this.cities = [];
           this.states = this.getStates().filter((item) => {
           return item.country_id === Number(country_id)
        });
    }
    getStates() {
        return this.http.get("https://api.myjson.com/bins/1a72vg").subscribe( 
          response => {}
          );
      }

【问题讨论】:

    标签: angular


    【解决方案1】:

    问题:您正在尝试返回订阅,而不是实际数据,然后对其应用过滤器(从显示的错误中非常清楚)。

    解决办法:

    getStates() 返回一个 Observable,然后在OnSelectCountry() 中订阅它。下一步是应用过滤器。

    onSelectCountry(country_id: number) {
            this.selectedCountry = country_id;
            this.selectedState = 0;
            this.cities = [];
               this.getStates().subscribe((states) => {
                    this.states = states.filter((item) => {
                        return item.country_id === Number(country_id)
                    });
              })
        }
    
    getStates() {
            return this.http.get("https://api.myjson.com/bins/1a72vg");
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 2011-01-21
      • 2013-06-28
      • 2011-01-07
      • 2017-08-27
      • 2021-02-06
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多