以角度2获得多次响应

时间:2017-08-19 17:33:45

标签: angular asynchronous angular-promise angular2-services angular2-observables

我是Angular2的新手并使用Http服务。我有一个调用Http服务并返回响应的组件。然后我订阅了它,它工作正常。 但问题是在这个调用中,我还检查了一些验证和如果我得到响应'null'那个时间验证会抛出一条错误消息,但是在添加组件时,即使我收到错误消息后所有数据都成功添加

这是我的服务

 getDataSync(url: string) {
    return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(JSON.parse(xhr.response));
                } else {
                    reject(xhr.response);
                }
            }
        }
        xhr.open("GET", url, false);
        xhr.setRequestHeader('Accept', 'application/json');
        xhr.send(null);
    });
}

以下是我调用该服务的组件。

  BlurEventCall(attribute) {
        if(!this.validationService.isValidattributeRequired(this.siteDetail.attribute)) {

            this.siteDetail.WeatherLocation = "";
            this.toastr.error("Please enter valid attribute");
        }
        else {
            if (this.siteDetail.SiteId == null || this.siteDetail.SiteId === undefined || this.siteDetail.SiteId == "") {
                this.weatherdetails = [];
            }
            this.siteLoading = true;
            this.queryString = "?attribute=" + attribute;

            return new Promise((resolve, reject) => {
                this.siteService.getStationByattribute(this.queryString).then(data => {
                    this.element = data;
                    let returnVal;
                    if (this.element == null) {
                        this.siteLoading = false;

                        this.toastr.error("Invalid attribute");
                        returnVal = true;

                    } else {
                        this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName;
                        if (this.siteDetail.StateName.toLowerCase() == this.element.response.place.stateFull.toLowerCase()) {

                            this.siteDetail.WeatherLocation = this.element.response.place.name;
                            this.isWeatherFlag = true;
                            this.siteLoading = false;
                            return false;

                        }

                        else {
                            this.siteLoading = false;

                            this.toastr.error("attribute does not match with the selected State");
                            returnVal = true;
                        }
                    }

                    resolve(data);
                    return returnVal;
                });

            });            
        }
    }

这里正在添加组件并得到两个响应。

Add() {


        if (!this.BlurEventCall(this.siteDetail.attribute)) {



            if (this.ValidateSiteModel(true) == "Valid") {
                this.siteLoading = true;               

                    console.log("zipcode - Valid");

                this.siteDetail.CountryName = this.countries.find(item => item.CountryId === this.siteDetail.CountryId).CountryName;
                this.siteDetail.StateName = this.states.find(item => item.StateId === this.siteDetail.StateId).StateName;
                this.siteDetail.CityName = this.cities.find(item => item.CityId === this.siteDetail.CityId).CityName;

                this.siteService.addSite(this.siteDetail).subscribe((response) => {
                    if (response === 1) {
                        this.toastr.success("Site added successfully");
                        this.siteLoading = false;
                        this.onCustomerSelect(this.siteDetail.CustomerId);
                    } else if (response === 2) {
                        this.toastr.error("Site already exists");
                        this.siteLoading = false;
                    }
                    else {
                        this.toastr.error("Site not added");
                        this.siteLoading = false;
                    }
                });
                this.clearSiteForm();
            }
            else {             
                this.toastr.error(this.ErrorMessage);
                this.siteLoading = false;
           }       

        }
    }

如何在角度2中阻止此类异步调用?

0 个答案:

没有答案