2017-06-14 84 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