在AngularJS和RequireJS中定义工厂

时间:2015-02-14 12:26:59

标签: javascript angularjs requirejs

我有一个处理服务的角度模块,如下所示

(function(){
define('rightClickEventHandler',['angular'],function(){
    angular.module('rightClickEventHandler',[])

    .factory('rightClickEventHandler',function(){
        var events ={};

        events.addMenu = function(){
            console.log('this is a add menu');
        };

        events.deleteMenu = function(){
            console.log('this is a delete menu');
        }

        return events;
    });
});
})();

使用'定义'因为我也使用requireJs,所有工作都很好,直到我的工厂名称为'rightClickEventHandler'与模块名称相同,但当我将名称更改为其他内容时,我会收到错误,如下所示

Error: [$injector:unpr] Unknown provider: rightClickEventHandlerProvider <- rightClickEventHandler <- topMenuRightClickCtrl
http://errors.angularjs.org/1.3.8/$injector/unpr?p0=rightClickEventHandlerProvider[object Object]3C-%rightClickEventHandler%%3C-%topMenuRightClickCtrl

有人可以建议出现什么问题吗?

1 个答案:

答案 0 :(得分:0)

得到答案,必须仔细阅读文档。

问题是在其他模块中注入服务期间

一个:

.controller('topMenuRightClickCtrl',['$scope','$http','commonData','rightClickEventHandler',
    //function to read the json from local computer 
    //this is to be replaced by the webservice call
    function($scope,$http,commonData,rightClickEventHandler){

我正在注入模块名称而不是服务名称

整改代码:

.controller('topMenuRightClickCtrl',['$scope','$http','commonData','ServiceName',
    //function to read the json from local computer 
    //this is to be replaced by the webservice call
    function($scope,$http,commonData,ServiceName){