如何从get方法调用变量到post方法?

时间:2018-04-02 15:22:41

标签: angularjs angularjs-directive angularjs-scope

我想将v = "'"+u+"'"值调用到post方法中,并将它们声明为全局变量。我无法在get方法之外控制这些值。它给我带来了错误。如何解决这个问题?

scope.Ex = function() {
            var u,w,v,c,is;

            $http.get("/api/patients/")
            .then(function(response) {
                scope.content = response.data;
                c = scope.content;
                is =c["0"].patient_ID;
                u = '/api/patients/'+is+'/';
                v = "'"+u+"'"

            });

            console.log(v);
            $http.post("/api/exams/",
                        {
                            patient_ID : v // I want to call V here from get method
                            // patient_ID : "/api/patients/1/"
                        })
                        .error(function(err){

                            //console.log(err);
                        })
                        .success(function(response) 
                        {
                            //console.log(v);
                            //console.log("Success")
                            //console.log(response);
                            //$scope.usersData = response;
                        });
        };

1 个答案:

答案 0 :(得分:-1)

链接GET和POST操作:

$http.get(url)
  .then(function(response) {
    return response.data;
}).then(function(data) {
    return $http.post(url,data);
})

它对return数据和承诺函数的承诺非常重要。

有关详细信息,请参阅AngularJS $q Service API Reference - Chaining promises