1

我正在做关于 angularjs 的教程。一切都很好,直到使用路线。我以前搜索过这个问题,但它对我不起作用。
我正在执行作者键入的代码,但它不起作用。ng-view 放入 index.html

<html ng-app="githubViewer">
<head>
    <title>Demo</title>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/angular-route.js"></script>
    <script type="text/javascript" src="app.js"></script>
    <script type="text/javascript" src="MainController.js"></script>
    <script type="text/javascript" src="github.js"></script>
</head>
<body>
    <h1>Github Viewer</h1>
    <div ng-view=""></div>
</body>

应用程序.js

(function() {
var app = angular.module("githubViewer", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
            .when("/main", {
                templateUrl: "main.html",
                controller: "MainController"
            })
                    .otherwise({redirectTo:"/main"});
});})();    

主控制器.js

(function() {
var app = angular.module("githubViewer");
var MainController = function(
        $scope, $interval, $location) {
    var decrementCountdown = function() {
        $scope.countdown -= 1;
        if ($scope.countdown < 1) {
            $scope.search($scope.username);
        }
    };
    var countdownInterval = null;
    var startCountdown = function() {
        countdownInterval = $interval(decrementCountdown, 1000, 5, $scope.countdown);
    };
    $scope.search = function(username) {
        if (countdownInterval) {
            $interval.cancel(countdownInterval);
            $scope.countdown = null;
        }
    };
    $scope.username = "angular";
    $scope.countdown = 5;
    startCountdown();
};
app.controller("MainController", MainController);})();

main.html

<div>
{{countdown}}
    {{username}}
    <form name="searchUser" ng-submit="search(username)">
        <input type="search" required placeholder="usẻname to ind" ng-model="username" />
        <input type="submit" value="Search" ng-click="search(username)">
    </form>

github.js

(function() {

var github = function($http) {

var getUser = function(username){
  return $http.get("https://api.github.com/users/" + username)
           .then(function(response){
               return response.data;
   });
};

    var getRepos = function(user){
      return $http.get(user.repos_url)  
              .then(function(response){
                  return response.data;
      });
    };

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

var module = angular.module("githubViewer");
module.factory("github", github);})();
4

1 回答 1

0

您的代码中有错误:

var MainController = function($scope, $interval, , $location) {
                   // unnecessary comma here ----^

删除逗号(或插入缺少的参数),您的应用程序应该开始工作。

一般来说,我建议在编码期间始终保持开发者控制台打开。

于 2014-11-05T09:36:27.083 回答