[英]$window.location will not reload page, angular
我正在嘗試重新加載管理頁面,我知道angular具有$window.location.reload()
和$route.reload()
。 我嘗試通過將$window
注入到函數的參數中來使用$window.location.reload()
scope.uploadList = function($window) {
//...
$window.location.href = /#/admin';
}
在成功Flash消息下方,但我仍然收到一條錯誤消息,內容為
Cannot read property 'location' of undefined
不知道從這里去哪里。 我已經在指令中添加了它。
您將需要在控制器中而不是在方法中注入$window
,它將起作用。
您應該在控制器代碼中注入$window
,如下所示
angular.module('windowExample', [])
.controller('ExampleController', ['$scope', '$window', function($scope, $window) {
$scope.greeting = 'Hello, World!';
$scope.doGreeting = function(greeting) {
$window.alert(greeting);
};
}]);
雖然沒有功能!
嘗試使用$location
。 這是用angularjs內置的,用於導航。 確保將其作為依賴項注入。
$location.path('/admin')
$window
不是使用全局引用所需的對象
window.location.href = '/#/admin';
將$ window注入到您的任何控制器中,如下所示:然后,您將可以訪問瀏覽器窗口對象。
angular
.module('app', [])
.controller('MainController', ['$window', function($window) {
var vm = this;
vm.hello = 'Hello, Universe!';
$window.alert(hello);
$window.location.reload();
$window.location.href = '/#/admin';
}]);
我使用$ location.path(“ url),但是如果您已經在該特定頁面上,它什么也不做,因此我在routing.js中做了第二條路由,該路由轉到同一頁面以影響“重新加載”頁面。我不確定這是否是您要的。
例:
.when('/Login',
{
templateUrl: 'Partials/LoginCustomer.html',
controller:'loginController'
})
.when('/Login2',
{
templateUrl: 'Partials/LoginCustomer.html',
controller:'loginController'
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.