路由在angularjs中不起作用

时间:2014-06-30 09:24:15

标签: javascript angularjs url-routing

我对角js很新。 我已经在角度js中实现了路由,但它没有将我重定向到我在route.js中声明的页面 这是代码: Route.js

var sampleApp = angular.module('sampleApp', []);

sampleApp.config(['$routeProvider',
  function($routeProvider) {
$routeProvider.
  when('/getplaces', {
    templateUrl: 'getplaces.html',
    controller: 'ListCtrl',

  }).
  when('/getuncategorisedplaces', {
    templateUrl: 'list.html',
    controller: 'uncatCtrl'
  })
  .otherwise ({
redirectTo: '/getplaces'
  });

}]);

Controller.js

  function uncatCtrl($scope, $http) {
  $http.get('http://94.125.132.253:8000/getuncategorisedplaces').success(function (data) {
  $scope.places = data;
   console.log(data);
    }
    )}
// get places
  function ListCtrl($scope, $http) {
  $http.get('http://94.125.132.253:8000/getplaces').success(function (data) {

   $scope.places = data;
  console.log("Successful")
 console.log(data);
  }
  )}

HTML代码包括ng视图和href,例如href =" #getplaces"

<body ng-app="sampleApp" >
<div class="container">
    <div class="row">
    <div class="col-md-3">
       <ul class="nav">
            <li> <a class= "linha" href="#getplaces"> Get places </a></li>
            <li><a class= "linha" href="post.html"> Post a movie </a></li>
                            <li><a class= "linha" href="#getuncategorisedplaces">List of uncategorised places  </a></li>
        </ul>
    </div>
    <div class="col-md-9">
        <div ng-view></div>

    </div>

    </div>

2 个答案:

答案 0 :(得分:1)

检查是否包含两个脚本(angular和ngroute)

<script src='angular.js'>
<script src='angular-route.js'>

然后检查您是否在该应用中包含该模块:

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

从Angular 1.2.0开始,angular-route是一个单独的模块

参考角度文档:

https://docs.angularjs.org/api/ngRoute

答案 1 :(得分:0)

这是我前一段时间编写的示例代码。

var app = angular.module('test', ['ngRoute','ngGrid','ngBootstrap', 'http-auth-interceptor','ui.bootstrap','ngCookies','ngSanitize']).
config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/sign-in', {templateUrl: 'partials/login/signin.html', controller: LoginCtrl}).
        when('/admin/sign-in', {templateUrl: 'partials/login/userManagementSignin.html', controller: LoginCtrl}).
        otherwise({redirectTo: '/dashboard'});
}]);