[英]Angular reload Template + Controller without using Routes
我想不使用$routeProvider
重新加载模板和连接的Controller,因为我的Path结构不适合.when(...)
。 我想使用$location.path()
来读取URL并设置所需的操作。
但是:如果URL的路径更改,则模板不会自动更新,也不会重新加载控制器。
有没有办法这样说?
angular.module('myApp').controller('ctrl').reload()
我建议看看由编写angular的同一个人编写的Angular UI Router。 它会让您过渡到不同的状态,这听起来像是您要尝试做的事情。
我的解决方案:不要使用路线! 只需为模板更新变量,控制器就不需要更新(只需更新数据):
angular.module('myApp',[])
.run(['$rootScope', '$location', 'myService', function ($rootScope, $location, myService) {
$rootScope.view = "";
// Some function to read the URL
$rootScope.setRouting = function(){
var p = $location.path().split("/");
// Do things with p[0], p[1], ...
// e.g. if url is /post/123
$rootScope.view = p[0];
myService.setData(p[1]);
}
// Monitor Location changes:
$rootScope.$on('$locationChangeSuccess', function(event) {
$rootScope.setRouting();
});
// And this is useful for calling within template: ng-click="go('...')"
$rootScope.go = function(path){
$location.path(path);
}
}]);
对于HTML:
<body>
<ng-include src="view + '.html'"></ng-include>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.