gpt4 book ai didi

angularjs - Angular 1.5 路由示例

转载 作者:行者123 更新时间:2023-12-04 14:42:37 24 4
gpt4 key购买 nike

我正在尝试使用 $route 找到一个 Angular 1.5 路由示例。如果我使用比用于创建代码示例更高的 Angular 版本,我查看的 JSFiddle 示例会中断。我找不到任何高于 1.2 的工作。理想情况下,我需要一个 AngularJS 1.5 的工作示例。

我已经尝试过this例子:

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 ) {
});

1.2 失败(大概在上面)。

最佳答案

AngularJS 1.2 发布时ngRoute已移至其自己的模块中。这意味着您需要包含一个单独的文件才能使路由在 AngularJS 1.2 之上工作。这意味着首先要做的是包含 angular-route.js文件,就像包含任何其他脚本一样

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

其次,包含对您的应用程序模块的依赖项。
var myApp = angular.module('myApp', ['ngRoute', 'otherModule']);

最后,您可以配置您的 $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>'
});
});

这就是路由背后的整个设置魔法。这是一个有效的 example .

编辑 1:

如果您使用 bower您可以使用以下内容获取模块:
bower install angular-route

关于angularjs - Angular 1.5 路由示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33257479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com