Javascript 路由不工作且未抛出错误
我尝试使用Javascript 路由不工作且未抛出错误,javascript,angularjs,angular-ui-router,angularjs-routing,Javascript,Angularjs,Angular Ui Router,Angularjs Routing,我尝试使用$routeProvider。但是出了点问题,我不知道是什么。控制台没有抛出错误,并且我的页面中没有发生任何事情。我很困惑。有什么解决办法吗 这是我的代码: var app = angular.module("admin-rv-app", ['ngRoute']); app.config(function($routeProvider) { $routeProvider // route for the home page
$routeProvider
。但是出了点问题,我不知道是什么。控制台没有抛出错误,并且我的页面中没有发生任何事情。我很困惑。有什么解决办法吗
这是我的代码:
var app = angular.module("admin-rv-app", ['ngRoute']);
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'templates/home.html',
controller : 'homeController'
})
// route for the thread page
.when('/thread', {
templateUrl : 'templates/thread.html',
controller : 'threadController'
});
});
app.controller('homeController', function($scope) {
$scope.message = "Welcome Home";
});
app.controller('threadController', function($scope) {
$scope.message = "Welcome to Thread";
});
这是我的HTML
<li>
<a href="#">
<i class="fa fa-home fa-3x" aria-hidden="true"></i>
<span class="title">Home</span>
<span class="selected"></span>
</a>
</li>
<li>
<a href="#thread">
<i class="fa fa-briefcase fa-3x" aria-hidden="true"></i>
<span class="title">Thread + Answer</span>
<span class="selected"></span>
</a>
</li>
顺便说一下,我已经包括了
angular.min.js
和angular.route.min.js
和ng app=“admin rv app”
我认为问题在于如何创建app.config
app.config(["$routeProvider", function($routeProvider){
$routeProvider.when('/', {
templateUrl : 'templates/home.html',
controller : 'homeController'
})
// route for the thread page
.when('/thread', {
templateUrl : 'templates/thread.html',
controller : 'threadController'
});
}])
改为试试这个,不要在模块中添加$scope,这只会给你更多的错误来处理
同时将标记改为“/thread”,而不是#
您需要为.when语句使用$routeProvider,否则,您无法为不同视图使用$routeProvider在控制器中插入$location服务 之后
<a ng-click="changeLocation()"/>
尝试在HTML中使用此选项:
<li>
<a href="#home">
<i class="fa fa-home fa-3x" aria-hidden="true"></i>
<span class="title">Home</span>
<span class="selected"></span>
</a>
</li>
<li>
<a href="#thread">
<i class="fa fa-briefcase fa-3x" aria-hidden="true"></i>
<span class="title">Thread + Answer</span>
<span class="selected"></span>
</a>
</li>
如果您注意到我已将URL从“/”更改为“/主页”
我检查了一下,它正在工作。如果模块中的
$scope
不起作用,请告诉我。请将
和
放入模块中,然后再试一次。它不起作用并重定向到页面线程。页面线程为“我看不到在HTML中添加的ng视图”是否添加了ng view div以动态加载模板?是的,ng view以动态加载模板在我的代码中不起作用。我在html中更改了“/thread”,但甚至重定向到页面未定义尝试切换到ng href=“/thread”,而不是我能想到的正常href,你在索引中设置了基本标记了吗?谢谢你的回答,但仍然不起作用,如果我使用$location.path,会显示许多错误。$location.path可以像$routeProvider一样重定向吗?$location服务与$routeProvider一起使用,或者如果您想使用当前代码,请参阅此FIDLE,这意味着使用
此代码在我这边有效。你能粘贴完整的HTML代码吗?
<li>
<a href="#home">
<i class="fa fa-home fa-3x" aria-hidden="true"></i>
<span class="title">Home</span>
<span class="selected"></span>
</a>
</li>
<li>
<a href="#thread">
<i class="fa fa-briefcase fa-3x" aria-hidden="true"></i>
<span class="title">Thread + Answer</span>
<span class="selected"></span>
</a>
</li>
app.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/home', {
templateUrl: 'app/dashboard/home.html',
controller: 'homeController'
})
// route for the thread page
.when('/thread', {
templateUrl : 'templates/thread.html',
controller : 'threadController'
});
});