Angular.js:未捕获的对象行36 ng路由

时间:2014-07-13 00:10:41

标签: javascript html angularjs ngroute

我正在学习AngularJS,以建立自己的网站。 kevhong.com我尝试使用ng-route制作导航栏。我按照http://viralpatel.net/blogs/angularjs-routing-and-views-tutorial-with-example/

中的示例进行操作

但是控制台一直在显示" Uncaught object"在第36行。我在网上搜索,但仍然无法修复错误。也许我想念一些东西,但我不知道。

我正在使用Angular 1.2.19

这是我的html文件:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script scr="index/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="index/index.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="index/index.css">
<link href='http://fonts.googleapis.com/css?family=Arvo:400,700,400italic,700italic' rel='stylesheet' type='text/css'>

</head>
<body ng-app="indexPage">   
<header>
    <ul class="nav nav-tabs">           
        <li><a href="#about-me"> About Me </a></li>
        <li><a href="#education"> Education </a></li>
        <li><a href="#skillAndProject"> Skills & Projects </a></li>
        <li><a href="#experience"> Experience </a></li>
    </ul>
</header> 
<div class="container">
    <div class="content" style="padding-left:15px; padding-right:15px;">
        <br>    
        <div class="ng-view"></div>
    </div>
</div>
</body>
</html>

我的JS档案:

/* angularJS implement */
(function(){
 var app = angular.module('indexPage',['ngRoute']);

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/about-me', {
        templateUrl: 'templates/about_me.html',
        controller: 'AboutMeController'
      }).
      when('/education', {
        templateUrl: 'templates/education.html',
        controller: 'EducationController'
      }).
      when('/skillAndProject', {
        templateUrl: 'templates/skillAndProject.html',
        controller: 'SkillAndProjectController'
      }).
      when('/experience', {
        templateUrl: 'templates/experience.html',
        controller: 'ExperienceController'
      }).
      otherwise({
        redirectTo: '/about-me'
      });
  }]);

//Controllers 
app.controller('IndexController', function(){
 });
app.controller('AboutMeController', function(){
});
app.controller('EducationController', function(){
    this.educations = educations;
    this.courses = courses;
});
app.controller('SkillAndProjectController', function(){
    this.skills = skills;
});
app.controller('ExperienceController', function(){
    this.experiences = experiences;
});

/*
JSON String Stores here
*/

})();

1 个答案:

答案 0 :(得分:0)

问题是你试图引用这些控制器范围内不存在的东西。因为你是棱角分明的新人,让我解释一下。你用来传递给EducationController的函数是一个回调,因此;你不能真正传递它的参数与经典的javascript工作方式相同。控制器不是像经典函数那样接受数据的东西,它控制着数据的访问。我邀请您阅读这篇文章:http://viralpatel.net/blogs/angularjs-controller-tutorial/

    // Doesn't work because you didn't inject anything into this controller
    app.controller('EducationController', function(){
        this.educations = educations;
        this.courses = courses;
    });
    // This would work if you had an educations, or courses module to inject.
    app.controller('EducationController', function(educations, courses){
        this.educations = educations;
        this.courses = courses;
    });