如何为angularjs路由提供href

时间:2016-06-16 07:56:50

标签: javascript angularjs

我是Angular.js的新手,我正在尝试了解ngRoute,但它没有显示任何内容

这是app.js配置文件

var app = angular.module('myApp',['ngRoute']);

// configure our routes
  app.config(function($routeProvider) {
    $routeProvider

        // route for the home page

        .when('/', {
            templateUrl : 'index.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'bb.html',
            controller  : 'loginController'
        })

        // route for the contact page
        .when('/contact', {
            templateUrl : 'contact.html',
            controller  : 'contactController'
        });
});

这是主要的index.html

    <head>
        <link href="css/bootstrap.min.css" rel="stylesheet" media="screen" />
        <link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen"/>
    </head>

        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular-route.min.js"></script>

 <body ng-app="myApp">


<h1> User :{{user}}</h1>


<a href="#about">
    about
</a>

<h1>User :{{user}}</h1>

    </html></body>
</html>

    <script src="app.js"></script>

<script src="mainController.js"></script>
<script src="loginController.js"></script>

这些是我的控制器

//mainController.js
     app.controller('mainController',function($scope, $http) {

      $scope.user = "user";
    });
//loginController.js
  app.controller('loginController',function($scope, $http) {


    $scope.user = "user";

});

这是

当我点击about时,它会给出CAN NOT GET / about,因为它会检查节点路径。我应该如何给出href?

1 个答案:

答案 0 :(得分:1)

因为你的html中有ng-app = "blog",而你的js中有angular.module("myApp",['ngRoute']);