1

I'm working on my first Angular.js application and I'm a bit confused. Currently I have two directives that both need the same data to build up the page. This data is loaded from an external api.

Now currently I have created this factory, which looks like:

(function() {
    var app = angular.module('dataService', []);
    app.factory('dataService', ['$http', function($http) {
        var links = [];

        return {
            getMenu: function() {
                if(links.length > 0) {
                    return links;
                } else {
                    $http.get('http://localhost/server/api.php?ajax=true&action=getCats').success(function(data) {
                        return data;
                    })
                }
            }
        }
    }])
})();

But I'm rather confused how to use this service, obviously if there is a $http request, the return will never be called with the correct data.

In my directive I would use it like this:

(function() {
    // Menu directive
    var app = angular.module('menu', ['dataService']);
    app.directive('menu', ['dataService', function(dataService) {
        return {
            restrict: 'E',
            templateUrl: 'scripts/menu/menu.html',
            controller: function() {
                console.log(dataService.getMenu()); // Return 'undefined'
            },
            controllerAs: 'menuCtrl'
        }
    }])
})();

1 Answer 1

2

Change your service method so that it handles both synchronous and asynchronous scenarios:

  getMenu: function() {
                 var deferred = $q.defer();
                if(links.length > 0) {
                   deferred.resolve(links);
                } else {
                    $http.get('http://localhost/server/api.php?ajax=true&action=getCats').success(function(data) {
                   deferred.resolve(data);
                    })
                }
           return deferred.promise;
   }

Usage:

dataService.getMenu().then(function(data){
  console.log(data);
});
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, that seems to work just fine. However, when I use it, am I correct that I should use var self = this; // In then() promise self.data = data?
@woutr_be You could just inject $scope in your directive controller function and say $scope.data = data. Tbh I get a bit confused with all that self=this malarkey.
Sorry, I'm a bit confused about how to use $scope, I tried injecting it into my directive controller, but I keep getting an error: Module '$scope' is not available! You either misspelled the module name or forgot to load it. pastebin.com/bDiUfum5
All the documentation about it is so inconsistent and there are different styles all over the place
@woutr_be There is no need to inject $scope into the module and directive definition itself, app.directive('menu', ['$scope',, remove $scope from there, just inject it to your controller function. Also remove it from here: angular.module('menu', ['$scope', 'dataService']);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.