gpt4 book ai didi

AngularJS 路由提供程序不起作用

转载 作者:行者123 更新时间:2023-12-02 23:06:48 25 4
gpt4 key购买 nike

当我尝试使用 $routeProvider 时遇到问题。它根本不起作用,我没有看到任何问题:

var app = angular.module('app', ['ngStorage','ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/index.html',
controller: 'authCtrl'
})
.when('/dashboard', {
templateUrl: '/tpl/dashboard.html',
controller: 'mainCtrl'
})
.otherwise({
redirectTo: '/'
});
}]);

这是index.html:

<html>
<head>
<title>Authenticate | BulbThings</title>
<script src="angular/angular.min.js"></script>
<script src="angular/ngStorage.min.js"></script>
<script src="angular/angular-route.min.js"></script>
<script src="js/build/app.min.js"></script>
</head>
<body ng-app="app">
<div ng-show="authenticate">
<span ng-cloak ng-show="addr" id="address">{{address}}</span>
<input type="mail" ng-model="email" placeholder="Email"><br />
<input type="password" ng-model="password" placeholder="Password"><br />
<button ng-click="auth.connect(email, password)" ng-model="signin">Sign in</button><br/><br/>
</div>
<p>{{error}}</p>
</body>
</html>

我在 Controller 中设置了$scope.authenticate = true,当我加载页面时它没有显示。

当我尝试/dashboard 时,它返回Cannot GET/dashboard

我看不出出了什么问题,希望你能帮助我!

谢谢。

最佳答案

有两件事,首先你需要 ng-view 来渲染 View

<div ng-view=""></div>

其次,如果您收到“Cannot GET/dashboard”是因为在 Angular 中,路由位于哈希中,例如:

yourdomain.com/#/dashboard,而不是 yourdomain.com/dashboard。因此,如果您想转到仪表板,这里有一个链接:

<a ng-href="#/dashboard">Dashboard</a>

关于AngularJS 路由提供程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508857/

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