我在角JS使用routeProvider:为什么不能在Angular JS中使用routeProvider?
.when('/chat/dialog/:id', {
controller: 'ChatController'
});
在控制我有:
if($routeParams.id) {
alert('ok');
}
而且我URL看起来就像是:
http://site-dev.com/chat/dialog/1
链接是:<a href="/chat/dialog/1"></a>
为什么不能在Angular JS中工作路由?
控制器:
.controller('ChatController', ['$scope', '$sce', '$http', '$location', '$anchorScroll', '$timeout', '$routeParams', function ($scope, $sce, $http, $location, $anchorScroll, $timeout, $routeParams,) {
if($routeParams.id) {
alert('ok');
}
}])
路由:
.config(function ($locationProvider, $routeProvider) {
$routeProvider
.when('/profile/personal/:type', {
templateUrl: '/public/html/personal.html',
controller: 'EditProfileController'
})
/* Chat */
.when('/chat/dialog/:id', {
controller: 'ChatController'
});
})
你能提供更详细一点编码?您的完整路线配置,您的控制器代码。另外,什么不行?路由?或警报?或者是其他东西?如果它是有条件的警报,那么猜测会是你没有将$ routeParams注入到控制器中 – o4ohel
不工作'alert()',我更新的问题增加了更多细节 – Hamed
看起来你在函数定义中有一个额外的逗号 - 函数($ scope,$ sce,$ http,$ location,$ anchorScroll,$ timeout,$ routeParams,)< - 这里 –