不要从表单Angular发送值

时间:2018-02-27 14:28:18

标签: angular rest http webpack promise

有没有办法不将值从表单发送到服务器?

我有更新请求,在这个函数中我不想发送hospital.name(我试图将它设置为null但它不能正常工作。)

有没有办法忽略这个值?

type(variable)

3 个答案:

答案 0 :(得分:2)

func getRequest() {

     let getUserID :UserDefaults = UserDefaults.standard

     let parameters: Parameters = [

        "user_id" : getUserID.value(forKey: "user_id")!

    ]

    Alamofire.request(URL_String, method: .post, parameters: parameters).responseJSON{


        (response) in


        print(" url :\(response.request)")

        guard response.result.isSuccess  else {

            print("Block 1 ")


            print(" response :\(response)")

            print("Error with response: \(String(describing: response.result.error))")
            return
        }
        guard let dict = response.result.value as? Dictionary <String,AnyObject>


            else {

            print(" response :\(response)")

            print("Error with dictionary: \(String(describing: response.result.error))")
            return
        }

        guard let dictData = dict["message"] as? [Dictionary <String,AnyObject>] else {

            print("Error with dictionary data: \(String(describing: response.result.error))")

            return
        }

        for data in dictData{

            self.lawNameArray.append(data["title"] as! String)
            self.lawImageArray.append(data["law_images"] as! String)
            self.dataArray.append(data["title"] as! String)
            print("lawNameArray..............\(self.lawNameArray)")
            print("lawImageArray..............\(self.lawImageArray)")

            self.lawIdArray.append(data["law_id"] as! String)
            self.lawStatusArray.append(data["status"] as! String)

            print("law Id Array :\(self.lawIdArray)")

            print("Data array :\(self.dataArray)")

        }
        self.myActivityIndicator.stopAnimating()
        self.myActivityIndicator.hidesWhenStopped = true
        self.collectionView.reloadData()
        print(" dict data\(dictData)")

    }


}

删除hospital.name; 将从对象中删除属性。

&#13;
&#13;
update(hospital: Hospital): Promise<Hospital> {
delete hospital.name;
    const url =`${this.hospitalsUrl}/${hospital.id}`;
    return this.http.put(url, JSON.stringify(hospital), {headers: this.headers})
    .toPromise()
    .then(() => hospital);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

最好的方法是创建一个新对象发送到服务器。

例如,要更新医院,您可以创建一个UpdateHospitalRequest,以及从表单到UpdateHospitalRequest的转换器,可以在您的HospitalService中使用。

这将确保即使您出于某种原因更改了表单,除非您决定更改请求,否则请求将始终相同。

答案 2 :(得分:0)

(function() {
  function makeIframe(adElement, callback) {
    var script = document.querySelector('#adscript');
    var iframe = document.createElement('iframe');
    script.type = "text/javascript"
    iframe.appendChild(script);

    adElement.appendChild(iframe);
    adElement.parentNode.insertBefore(iframe, adElement);
  }

}());

试试这个