0

I have the following preloader.js factory file included in my project.

    angular.module('movieSeat')
        .factory('preloader', function( $q, $rootScope ) {

In my controller I inject the factory as following:

    angular.module('movieSeat')
        .controller('moviesearchCtrl', ['$scope', 'moviesearchFactory', function ($scope, moviesearchFactory, preloader) {

When I try to use the preloadImages function inside the preload controller:

    $scope.imageLocations = [
        "https://image.tmdb.org/t/p/w300_and_h450_bestv2//nlqFgG7jfBITl8fJHzL72jXwYLt.jpg",
        "https://image.tmdb.org/t/p/w300_and_h450_bestv2//ghL4ub6vwbYShlqCFHpoIRwx2sm.jpg",
        "https://image.tmdb.org/t/p/w300_and_h450_bestv2//weUSwMdQIa3NaXVzwUoIIcAi85d.jpg",
    ];

    preloader.preloadImages($scope.imageLocations)

I get the following error:

Cannot read property 'preloadImages' of undefined

If I comment out the preloader.preloadImages($scope.imageLocations) code the error is gone. So to me it looks like injecting the preloader factory in my controller works fine but I can't figure out why I can't call the function preloadImages on the preload factory.

1
  • show us the preloadImages function Commented Jul 18, 2016 at 10:40

2 Answers 2

2

There's a typo in the dependency injection list. You forgot to add preloader in the list of dependency strings.

 .controller('moviesearchCtrl', ['$scope', 'moviesearchFactory', 'preloader' /*here*/, function ($scope, moviesearchFactory, preloader) {
Sign up to request clarification or add additional context in comments.

Comments

1

You need to reference your factory/service 'preloader' in your controller call as follows:

angular.module('movieSeat')
        .controller('moviesearchCtrl', ['$scope', 'moviesearchFactory', 'preloader', function ($scope, moviesearchFactory, preloader) {

})

Comments

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.