2017-06-14 82 views
0

我有一个问题,从另一个文件中的控制器内的一个文件注册服务。从控制器中的不同文件注册服务

我有被称为主文件"script.js",它看起来像这样:

(function() { 
    var app = angular.module("myApp", []); 

    var MainController = function(scope, github, interval, log, anchorScroll, location) { 
     //some code which is not important here 
    }; 

    app.controller("MainController", ["$scope", github, "$interval", "$log", "$anchorScroll", "$location", MainController]); 
}()); 

还有第二个文件名为"github.js"

(function() { 

    var github = function($htpp) { 

    var getUser = function(username) { 
     //return user data 
    }; 

    var getRepos = function(user) { 
     //return repo data 
    } 

    return { 
     getUser: getUser, 
     getRepos: getRepos 
    }; 
    }; 

    var module = angular.module("myApp"); 
    module.factory("github", github); 

}()); 

而且它不工作...我有不知道我错过了什么...我在看教程,但它是在Angular的旧版本上准备的...

当然,在.html文件引用"script.js"之前是"github.js"

编辑: 我错过了在GitHub上的依赖双引号和“github.js”有拼写错误 - $ HTPP而不是$ HTTP ...

+0

你是什么意思不工作?你有什么错误吗? –

+2

'github'必须用双引号,''github''依赖。 – anoop

+1

同样在github.js中,依赖关系应该是'$ http'而不是'$ htpp' –

回答

1

,我们在您的代码几个错别字:

1. github依赖必须是在双引号,"github"的依赖,就像:

app.controller("MainController", ["$scope", "github", "$interval", "$log", "$anchorScroll", "$location", MainController]); 

2.As建议:在控制器,dependecy应该是相同的(缺少$),做它喜欢:

var MainController = function($scope, github, $interval, $log, $anchorScroll, $location) { 

3.In你的工厂的依赖,应该是$http(不HTPP)

+0

关于第二点...为什么它应该与美元符号?它的工作没有它......我认为在那里我可以称它为任何我想... – Lui

+0

这是一个建议,它应该是相同的。 – anoop