无法将值从工厂返回到服务angularJS

时间:2023-01-24 11:15:03

my factory code

我的工厂代码

.factory('appData', ['connectionManager', function($connectionManager) {
    return {
        getAppDataItem: function(apiName, params) {
            console.log('Getting value...');
            $connectionManager.sendRequest({
                'apiName': apiName,
                'params':{
                    '@key': "'" + params['@key'] + "'"
                }
            }).then(function(res){
                console.log('Retrieved data ', res.data);
                return res.data;
            });
        }
    }
}]);

my service code

我的服务代码

            var x = appData.getAppDataItem('getItemFromAppData', {
                    '@key': 'last_emp_id'
                });
            console.log(x);

I want to assign value returned by .then() in factory to the var x in my service. But I am unable to do it. please help me. I am getting value up to .then(). After that I am unable to pass it to the service.

我想将工厂中.then()返回的值赋给我服务中的var x。但我无法做到。请帮帮我。我的价值高达.then()。之后我无法将其传递给服务。

1 个解决方案

#1


1  

The getAppDataItem function needs to return a promise:

getAppDataItem函数需要返回一个promise:

.factory('appData', ['connectionManager', function($connectionManager) {
    return {
        getAppDataItem: function(apiName, params) {
            console.log('Getting value...');                
            //$connectionManager.sendRequest({
            //return promise
            return $connectionManager.sendRequest({
                'apiName': apiName,
                'params':{
                    '@key': "'" + params['@key'] + "'"
                }
            }).then(function(res){
                console.log('Retrieved data ', res.data);
                return res.data;
            });
        }
    }
}]);

The client code should use the promise's .then method to access the data.

客户端代码应使用promise的.then方法来访问数据。

var promise = appData.getAppDataItem('getItemFromAppData', {
        '@key': 'last_emp_id'
    });
promise.then( function (x) {
    console.log(x);
});

#1


1  

The getAppDataItem function needs to return a promise:

getAppDataItem函数需要返回一个promise:

.factory('appData', ['connectionManager', function($connectionManager) {
    return {
        getAppDataItem: function(apiName, params) {
            console.log('Getting value...');                
            //$connectionManager.sendRequest({
            //return promise
            return $connectionManager.sendRequest({
                'apiName': apiName,
                'params':{
                    '@key': "'" + params['@key'] + "'"
                }
            }).then(function(res){
                console.log('Retrieved data ', res.data);
                return res.data;
            });
        }
    }
}]);

The client code should use the promise's .then method to access the data.

客户端代码应使用promise的.then方法来访问数据。

var promise = appData.getAppDataItem('getItemFromAppData', {
        '@key': 'last_emp_id'
    });
promise.then( function (x) {
    console.log(x);
});