使用角度js工厂中的函数

时间:2017-12-22 09:43:58

标签: javascript angularjs angular-promise

我从工厂的rest api获取一个值,在控制器中解析它的承诺,然后我再次使用工厂的$ http在控制器中发送以前检索的数据。看看代码,我遇到的问题是,在执行此操作时,第二个异步功能不起作用。我在这做错什么吗?在Json中传递像这样的数据也是一种很好的做法,即传递一个函数吗?

Screenshot of console

app.factory('PatientSummaryFactory2', function(\$http){
var patient = "${ patient.uuid }";
var url1 = "/" + OPENMRS_CONTEXT_PATH + "/ws/rest/v1/encounter";
var date2 = new Date();
return {
 getdata: function(){
    var providerUrl = "/" + OPENMRS_CONTEXT_PATH + "/ws/rest/v1/session";
    return \$http.get(providerUrl).then(function(response){
    return response.data.user.uuid;  // PASS THIS DATA INTO JSON TO POST
  });
 }
};

var json = {
  patient: patient,
  encounterType: window.constantConfigObj.encounterTypeVisitNote,
  encounterProviders: [{
   provider: getdata(),  //IS THIS A GOOD PRACTICE?
   encounterRole: "73bbb069-9781-4afc-a9d1-54b6b2270e03"
 }],
  visit: visitId,
  encounterDatetime: date2
};
console.log(json);
return {
async: function(){
  return \$http.post(url1, JSON.stringify(json)).then(function(response){
    return response.data.uuid;
   });
  }
 };
});

app.controller('PatientSummaryController', function(\$scope, \$http, 
PatientSummaryFactory1, PatientSummaryFactory2, recentVisitFactory) {
PatientSummaryFactory2.getdata().then(function(response){
    console.log(response);   //RESOLVING THE PROMISE
});
\$scope.isLoading = true;
\$scope.visitEncounters = [];
\$scope.visitObs = [];
\$scope.visitNoteData = [];
\$scope.visitStatus = false;
 recentVisitFactory.fetchVisitDetails(visitId).then(function(data) {
                    \$scope.visitDetails = data.data;
                    \$scope.visitEncounters = data.data.encounters;
                    if(\$scope.visitEncounters.length !== 0) {
                        angular.forEach(\$scope.visitEncounters, 
  function(value, key){
                            var encounter = value.display;
                            if(encounter.match("Visit Note") !== null) {
                                var encounterUuid = value.uuid;
                                visitNoteEncounterUuid = encounterUuid;
                                isVisitNotePresent = true;
                            }
                        });
                    }
                    if (isVisitNotePresent == false || 
                    \$scope.visitEncounters.length == 0) {

                    PatientSummaryFactory2.async().then(function(d2){ 
                                    \$scope.data2 = d2;
                                    visitNoteEncounterUuid = d2;
                      console.log(\$scope.data2);
                                }); 

                    }
                }, function(error) {
                    console.log(error);
                });
            });

1 个答案:

答案 0 :(得分:0)

您不能拥有多个(在您的情况下为2个)return来电。您的错误表明async不是函数,因为它先前返回 ed getdata,并且不会考虑单独返回任何其他函数。

您有多种方法可以解决此问题:

在一个return

中合并函数
return {
  getdata: function(){
    ...
  },
  async: function(){
    ...
  }
};

或者:

function getdata(){...}
function async(){...}
return {
  getdata: getdata,
  async: async
};

返回函数对象

var service = {
  getdata: function(){...},
  async: function(){...}
};
return service;

或者:

var service = {};
service.getdata = function(){...}
service.async = function(){...}
return service;

或者:

function getdata(){...}
function async(){...}
var service = {
  getdata: getdata,
  async: async
};
return service;