Loading data from the factory in my controller

I am using the factory to do my API calls and I would like them to happen before the rest of my controller happens.

Here is the code of the factory:

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.");
            });
        }
    }
});
});

I am trying to call my factory before the rest of the controller is executed:

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");
    }


});

});

I always get logged:

"promise failed"

What am I doing wrong?

Your getData method is also doing asynchronous http calls. getData returns nothing and your condition always failed. If you want to execute other controller stuff only when all http calls are succeceeded you should pass your promise inside getData. It may look like:

module.registerFactory('httpApiCallService', function ($http, $q)
{
    var assetsInUse, assetsCoupled;
    var api = { assetsInUse: null, assetsCoupled: null };
    return {

        getData: function(promiseToResolve) //Here we are sending promise to resolve when everything will be ready
        {
            $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);
                 //instead of returning api we resolving the promise with 'api'
                    promiseToResolve.resolve(api);

                }).error(function (data, status, headers, config)
                {
                    promiseToResolve.reject();
                    return alert("Something went wrong.");
                });
            }).error(function (data, status, headers, config)
            {
              promiseToResolve.reject();
              return alert("Something went wrong.");
            });
        }
    }
});
});

and in your controller:

var defer = $q.defer();

httpApiCallService.getData(defer) ;

defer.promise.then(function (api) {
    console.log('we are doing stuff and can use api received from backend');
})
.catch(function (){
    console.log("getData failed and we cannot continue");
});