Angular 1.5路由示例

Lau*_*ass 6 routing angularjs

我试图找到一个使用$ route的角度1.5路由示例.如果我使用比创建代码示例更高的角度版本,我已经看过JSFiddle示例.我找不到1.2以上的工作.理想情况下,我需要一个AngularJS 1.5的工作示例.

我已经尝试过这个例子:

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

app.config( function ( $routeProvider ) {
  $routeProvider
  .when( '/this', { templateUrl: 'this.html' } )
  .when( '/that', { templateUrl: 'that.html' } )
  .when( '/other', { templateUrl: 'other.html' } )
  .otherwise( { redirectTo: '/this' } );
});

app.controller( 'MainCtrl', function ( $scope ) {
});
Run Code Online (Sandbox Code Playgroud)

哪个失败了1.2(并且可能是上面的).

Lor*_*ual 7

当AngularJS 1.2发布时ngRoute已经被移动到自己的模块中.这意味着您需要包含一个单独的文件以使路由工作在上面并且等于AngularJS 1.2.这意味着要做的第一件事就是包含angular-route.js文件,就像包含任何其他脚本一样

<script src="angular-route.js">
Run Code Online (Sandbox Code Playgroud)

其次,包括对您的app模块的依赖.

var myApp = angular.module('myApp', ['ngRoute', 'otherModule']);
Run Code Online (Sandbox Code Playgroud)

最后,您可以$routeProvider像在上面的代码中一样配置:

app.config(function($routeProvider){
    $routeProvider
      .when('/',{
          template: '<h1>Work</h1><a href="#/test">Test Route</a>'
      })
      .when('/test',{
          template: '<h1>Test</h1><a href="#/">Back</a>'
      })
      .otherwise({ 
        template: '<h1>Not Found</h1>'
      });
});
Run Code Online (Sandbox Code Playgroud)

这就是路由背后的整体设置魔力.这是一个有效的例子.

编辑1:

如果您正在使用,bower您可以获得具有以下内容的模块:

bower install angular-route
Run Code Online (Sandbox Code Playgroud)