0
我正在使用工厂来完成我的API调用,并且希望在我的控制器的其他部分发生之前发生这些调用。从我的控制器的工厂中加载数据
这里是工厂代码:
define(['dashboard/module', 'lodash'], function (module)
{
'use strict';
module.registerFactory('httpApiCallService', function ($http, $q)
{
var assetsInUse, assetsCoupled;
var api = { assetsInUse: null, assetsCoupled: null };
return {
getData: function()
{
$http.get('/v2/api/inventory/assets/count').success(function (data, status, headers, config)
{
var totalAssets = data;
assetsInUse = { total: data, setup: null };
assetsCoupled = { total: data };
$http.get('/v2/api/usage/unused/count').success(function (data, status, headers, config)
{
assetsInUse.setup = [
{
value: totalAssets - data,
color: "#1675a9",
highlight: "#1675a9",
label: "is in use"
},
{
value: data,
color: "#7eb3cf",
highlight: "#1675a9",
label: "is not used"
}
]
api.assetsInUse = assetsInUse;
api.assetsCoupled = assetsCoupled;
console.log(api);
return api;
}).error(function (data, status, headers, config)
{
return alert("Something went wrong.");
});
}).error(function (data, status, headers, config)
{
return alert("Something went wrong.");
});
}
}
});
});
我想控制器的其余部分之前打电话给我厂执行:
define(['dashboard/module', 'lodash'], function (module, _) {
'use strict';
module.registerController('DashboardCtrl', function ($scope, $interval, $controller, $http, $q,
SmartMapStyle, uiGmapGoogleMapApi, SmartMapInstances, httpApiCallService)
{
//Data
var defer = $q.defer();
defer.promise.then(function() {
console.log('we are doing stuff');
});
if (httpApiCallService.getData())
{
defer.resolve();
}
else
{
console.log("promise failed");
}
});
});
我总是登录:
“的承诺失败”
我在做什么错?
你得到在控制台的任何错误? – Carnivorus 2015-03-13 13:00:46
不,没有关于这部分的应用程序的任何错误 – Sidneyvp 2015-03-13 13:03:13
你的getData()方法不返回任何东西,所以你总是得到未定义,这是虚假的。 – 2015-03-13 13:51:10