AngularJs:重新加载页面
我想重新加载页面。如何执行此操作?您可以使用AngularJs:重新加载页面,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我想重新加载页面。如何执行此操作?您可以使用$route服务的重新加载方法。在控制器中注入$route,然后在$scope上创建一个方法重载route <a ng-href="#" class="navbar-brand" title="home" data-translate>PORTAL_NAME</a> 然后您可以在链接上使用它,如下所示: $scope.reloadRoute = function() { $route.reload(); } 以后编辑
$route
服务的重新加载
方法。在控制器中注入$route
,然后在$scope
上创建一个方法重载route
<a ng-href="#" class="navbar-brand" title="home" data-translate>PORTAL_NAME</a>
然后您可以在链接上使用它,如下所示:
$scope.reloadRoute = function() {
$route.reload();
}
以后编辑(ui路由器):
正如JamesedYedwards和Dunc在他们的回答中提到的,如果您正在使用,可以使用以下方法重新加载当前状态/路由。只需注入
$state
而不是$route
,您就可以:
$scope.reloadRoute = function() {
$window.location.reload();
}
只需使用
$route.reload()
(不要忘记将$route注入控制器)就足够简单了,但是从您的示例中,您可以只使用“href”而不是“ng href”:
您只需使用ng href保护用户不受Angular在替换{}标记内容之前单击所导致的无效链接的影响。
window
对象可通过$window
服务获得,您可以使用以下内容:
<a href="" class="navbar-brand" title="home" data-translate>PORTAL_NAME</a>
以及:
$scope.reloadPage = function(){$window.location.reload();}
与Alexandrin的答案类似,但使用$state而不是$route:
(来自JimTheDev的回答。)
$scope.reloadState=function(){
$state.go($state.current,{},{reload:true});
}
如果使用我推荐的Angulars更高级,那么您现在只需使用:
$scope.reloadState = function() {
$state.go($state.current, {}, {reload: true});
}
<a ng-click="reloadState()" ...
这与邓克的答案基本相同
$state.reload();
这就是诀窍
location.reload();
$scope.reload=函数()
{
location.reload();
}
在Angular 1.5上,不需要路由或任何东西,只需要简单的旧js——在尝试了上面的一些解决方案后,我只想在不刷新整页的情况下重新加载数据,但在正确加载数据时遇到了问题。但是我注意到,当我转到另一个路由,然后返回到当前路由时,一切正常,但是当我只想使用$route.reload()
重新加载当前路由时,一些代码没有正确执行。然后,我尝试通过以下方式重定向到当前路由:
<a ng-click="reload()">
$scope.reload = function()
{
location.reload();
}
在模块配置中,添加另一个,当
:
$scope.someFuncName = function () {
//go to another route
$location.path('/another-route');
};
这对我来说很好。它不会刷新整个页面,只会导致重新加载当前控制器和模板。我知道这有点麻烦,但那是我找到的唯一解决办法
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/first-page', {
templateUrl: '/first-template',
controller: 'SomeCtrl'
}).when('/another-route', {//this is the new "when"
redirectTo: '/first-page'
});
}])
在路由文件中
$scope.viewPendingApprovals = function(type) {
if (window.location.hash.substring(window.location.hash.lastIndexOf('/') + 1, window.location.hash.length) == type) {
location.reload();
} else {
$state.go("home.pendingApproval", { id: sessionStorage.typeToLoad });
}
};
因此,如果在url中传递的id与通过单击锚点调用的函数的id相同,则只需重新加载,否则返回请求的路由
如果这有帮助,请帮助我改进这个答案。欢迎任何建议。我避免无限循环的解决方案是创建另一个导致重定向的状态:
.state('home.pendingApproval', {
url: '/pendingApproval/:id',
templateUrl: 'app/components/approvals/pendingApprovalList.html',
controller: 'pendingApprovalListController'
})
角度2+
我在搜索Angular 2+时发现了这一点,下面是方法:
$stateProvider.state('app.admin.main', {
url: '/admin/main',
authenticate: 'admin',
controller: ($state, $window) => {
$state.go('app.admin.overview').then(() => {
$window.location.reload();
});
}
});
这可以通过在纯JavaScript中调用窗口对象的reload()方法来实现
$window.location.reload();
我建议参考这一页。官方建议
这可以通过在JavaScript中调用reload()方法来完成
window.location.reload();
这是一个很好的答案,如果你不顾一切地想改变重新加载的角度。我怎样才能在全局范围内改变这种行为,而不是在所有操作中添加一个操作links@OMGPOP你所说的所有链接的全局性到底是什么意思?我的意思是,对于所有链接,你都需要这样做。是否可以为所有链接配置一次?@AlexandrinRus这给了我以下[$rootScope:infdig]10$digest()迭代次数。流产!喜欢简单!它打破了karma/jasmine测试,因为它会完全重新加载页面,并且不可模仿。通过使用$window
提供程序,可以在测试中进行模拟。我建议使用$window
而不是window
。感谢您的贡献。你能解释一下为什么吗?是的,对不起。最好的解释是,这主要是因为reloadPage
在使用$window
$route是ng.route.IRouteService的一部分时可以(更好地)测试。我花了一个月的时间才弄明白为什么页面不能重新加载一个更简单的方法可能是var reload=$location.url()$location.url(“”)$超时(函数(){$location.url(重载);})代码>;无需创建随机路由。但是,看看哪些代码没有为您重新计算会很有趣。它看起来甚至像重定向到
s和解析
函数(查看console.log)。顺便说一句,这不是特定于angular的javascript方式。
.state('home.pendingApproval', {
url: '/pendingApproval/:id',
templateUrl: 'app/components/approvals/pendingApprovalList.html',
controller: 'pendingApprovalListController'
})
$stateProvider.state('app.admin.main', {
url: '/admin/main',
authenticate: 'admin',
controller: ($state, $window) => {
$state.go('app.admin.overview').then(() => {
$window.location.reload();
});
}
});
$window.location.reload();
window.location.reload();
location.reload();