DwarakaT
DwarakaT

Reputation: 11

Angularjs injecting factory with http to a controller

I keep getting errors while injecting a factory in to a controller. It worked in a case where the controller is simple. It fails when the page and the controller has special components like ng-flow or ng-grid.

var carouselController = carouselApp.controller('carouselController', ['CMSFactory', function ($scope,CMSFactory) {

// MY CODE HERE...

}
var CMSServices = angular.module('api.services', [ 'ngResource' ]);

CMSServices.factory('CMSFactory', function($http, $q) {
    var CMSService = {};

    CMSService.saveSiteInfo = function(data) {

    // MY CODE HERE...

    };

    CMSService.addSlide = function(data) {

    // MY CODE HERE...

    };

    return CMSService;
});

I get TypeError: undefined is not a function error. If I remove the factory injection code works fine.

Appreciate any help...

Upvotes: 0

Views: 42

Answers (1)

tymeJV
tymeJV

Reputation: 104775

You're not declaring $scope in your array of dependencies:

Change:

var carouselController = carouselApp.controller('carouselController', ['CMSFactory', function ($scope,CMSFactory) {

To:

var carouselController = carouselApp.controller('carouselController', ['$scope', 'CMSFactory', function ($scope,CMSFactory) {

Upvotes: 1

Related Questions