Angularjs向工厂注入http到控制器

时间:2014-08-13 20:36:19

标签: angularjs

在将工厂注入控制器时,我一直遇到错误。它适用于控制器简单的情况。当页面和控制器具有ng-flow或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;
});

我得到TypeError:undefined不是函数错误。如果我删除工厂注入代码工作正常。

感谢任何帮助...

1 个答案:

答案 0 :(得分:1)

您未在您的依赖项数组中声明$scope

变化:

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

致:

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