Angularjs 随机应用程序与ngRoute崩溃
我遵循AngularJS ngView页面上的指南,但根据我的特定应用定制了它。当我尝试更改视图时,应用程序崩溃 相关代码如下:Angularjs 随机应用程序与ngRoute崩溃,angularjs,Angularjs,我遵循AngularJS ngView页面上的指南,但根据我的特定应用定制了它。当我尝试更改视图时,应用程序崩溃 相关代码如下: app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){ $routeProvider.when('/', { templateUrl: '/templates/items.html', con
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
$routeProvider.when('/', {
templateUrl: '/templates/items.html',
controller: 'itemListController',
controllerAs: 'list'
}).when('/Item/:itemID', {
templateUrl: '/templates/item.html',
controller: 'itemController',
controllerAs: 'item'
});
$locationProvider.html5Mode(true);
}]);
页面加载时,“项目”视图工作正常。但是,当我将$location.path
更改为/Item/1
时,应用程序会使浏览器崩溃。如果我将templateUrl
更改为相同的/templates/items.html
,它会正确更改,但显然无法正常工作,因为没有为该控制器设置模板
目前,我的item.html
temnplate是一个空文件。我尝试过添加一些东西,但运气不好。同样,我的itemController
也是一个空控制器。然而,当我注入$routeParams
依赖项时,我得到一个无限循环和“超过最大调用堆栈大小限制”错误。下面是我的无限循环控制器:
app.controller('itemController', ['itemService', '$routeParams', function(itemService, $routeParams){
console.log($routeParams);
}]);
app.controller('viewController', ['$route', '$routeParams', '$location', function($route, $routeParams, $location){
var ctrl = this;
ctrl.title = "App Name";
ctrl.$route = $route;
ctrl.$location = $location;
ctrl.$routeParams = $routeParams;
ctrl.changeView = function(view){
$location.path(view);
};
}]);
我的默认/
视图也有类似的问题,但不知怎么的,它还是正常工作了。几个小时后,我一直无法解决这个问题。我有没有做错什么?或者问题在我的代码中更深层
更新
根据注释中的要求,我更改的控制器$location.path
是我的主“视图”控制器:
app.controller('itemController', ['itemService', '$routeParams', function(itemService, $routeParams){
console.log($routeParams);
}]);
app.controller('viewController', ['$route', '$routeParams', '$location', function($route, $routeParams, $location){
var ctrl = this;
ctrl.title = "App Name";
ctrl.$route = $route;
ctrl.$location = $location;
ctrl.$routeParams = $routeParams;
ctrl.changeView = function(view){
$location.path(view);
};
}]);
然后在元素的内部调用
changeView()
”ng click
。例如,items.html
中的一个项目具有ng click=“view.changeView('/Item/'+Item.Id)”
您在控制台中得到的错误是什么?@sinu没有错误。我看到Chrome“哇,快照!显示此网页时出错。”崩溃。它似乎在IE中工作正常,目前无法在FireFox中测试;既然启用了HTML5模式,那么就设置了服务器端URL重写。我会检查浏览器网络控制台,确保HTML模板请求返回正确的数据,并且没有被意外重写。我还假设您在主HTML文件中正确设置了
标记。@Phil网络调用是针对相应的文件的。虽然我确实看到angular.js文件的404(即使在堆栈的更高位置,相同的文件也被正确加载…),但我正在从我的域(domain.com/test)的子文件夹中运行这个应用程序,并且在我的应用程序的头中有