0
我試圖讓這段代碼工作時遇到困難。我正在嘗試遵循來自pluralsight的教程,但想要將應用程序文件,控制器文件和服務文件拆分爲不同的文件。這樣做後,我的代碼不再起作用。我錯過了什麼來使其正常工作?將控制器注入應用程序?
http://plnkr.co/edit/7Mwb1M2e9KvG8zlIDmEx?p=preview
AppFile:
do ->
app = angular.module("githubViewer", [])
控制器的文件:
do ->
MainController =($scope, github, $interval, $log, $location, $anchorScroll) ->
vm = this
vm.username = "angular"
vm.sortField = "+language"
vm.message = "Github Viewer"
vm.countdown = 5
onUserComplete =(data) ->
vm.user = data
github.getRepos.then(onRepos, onError)
vm.noError = true
return
onRepos =(response) ->
vm.user.repos = response.data
$location.hash("userDetails")
$anchorScroll()
onError =(reason) ->
vm.message = "Could not fetch user"
vm.noError = false
return
vm.search =() ->
$log.info("Searching for " + vm.username)
github.getUser(vm.username).then(onUserComplete, onError)
if(countdownInterval)
$interval.cancel(countdownInterval)
vm.countdown = null
return
decrementCountdown =() ->
vm.countdown -= 1
if(vm.countdown < 1)
vm.search()
return
countdownInterval = null
startCountdown =() ->
countdownInterval = $interval(decrementCountdown, 1000, vm.countdown)
return
startCountdown()
return
app = angular.module("githubViewer")
app.controller("MainController", ["$scope", "github", "$interval",
"$log", "$location", "$anchorScroll", MainController])
服務文件:
do ->
github =($http) ->
getUser =(username) ->
return $http.get("https://api.github.com/users/" + username)
.then ->
return response.data
getRepos =(user) ->
return $http.get(vm.user.repos_url)
.then ->
return response.data
return {
getUser: getUser,
getRepos: getRepos
}
app = angular.module("githubViewer")
app.factory("github", ["$http", github])
return
我在你指定的plunker鏈接中運行代碼,代碼確實運行 - 我得到「無法獲取用戶」。 – 2014-09-11 03:38:01
以前,他們把所有的角碼放在一個文件中,它正在工作,並且能夠正確地獲取用戶。並且在控制檯中出現錯誤,表示響應未定義。 – user4029197 2014-09-11 22:25:30