Angularjs $location未重定向到页面
我正在尝试重定向到我的ionic应用程序中的另一个页面,但在这一点上卡住了。我在控制台中看到,我的当前路径是我想要的,但我并不是真的在那个页面上,这里是may代码Angularjs $location未重定向到页面,angularjs,cordova,ionic-framework,Angularjs,Cordova,Ionic Framework,我正在尝试重定向到我的ionic应用程序中的另一个页面,但在这一点上卡住了。我在控制台中看到,我的当前路径是我想要的,但我并不是真的在那个页面上,这里是may代码 .config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) { $ionicConfigProvider.views.maxCache(0); $stateProvider.state('homepage', {
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$ionicConfigProvider.views.maxCache(0);
$stateProvider.state('homepage', {
url: '/homepage',
templateUrl: 'templates/homepage.html',
controller: 'MapController'
})
.state('hotel', {
url: '/hotel',
templateUrl: 'templates/hotel.html',
controller: 'HotelController'
})
})
控制器
.controller('MapController', function($scope, $location, customservice) {
$scope.fa = function(a) {
customservice.hotel_name = {hotelName: a.innerHTML}
console.log($location.path()) // prints /homepage
$location.path('/hotel');
console.log($location.path()) // print /hotel but still on same page
}
});
我不知道如何和为什么,但在$timeout下包装它为我做了这项工作我不知道如何和为什么,但在$timeout下包装它为我做了这项工作美元定位服务只允许您更改URL;有时它不会重新加载/重定向页面。您可以试试$window.location.href或$state.go(),而不是$location.path()。可能会有用。$location服务只允许您更改URL;有时它不会重新加载/重定向页面。您可以试试$window.location.href或$state.go(),而不是$location.path()。也许它会起作用。这是一件非常令人困惑的事情$位置仅更改url而不重新加载。 如果你想重定向,你应该这样写
$window.location.href = "http://www.google.com"
请参阅
这是一件非常令人困惑的事情$位置仅更改url而不重新加载。 如果你想重定向,你应该这样写
$window.location.href = "http://www.google.com"
请参阅
请尝试以下内容:
angular.module('windowExample', [])
.controller('ExampleController', ['$scope', '$window', function($scope, $window) {
$window.location.href = '/hotel.html';
};
}]);
请尝试以下内容:
angular.module('windowExample', [])
.controller('ExampleController', ['$scope', '$window', function($scope, $window) {
$window.location.href = '/hotel.html';
};
}]);
为什么不改为使用$location.url()?仍然得到相同的结果SAM,注入$state并使用$state.go()在状态之间转换,看看是否有效。不知道这是否是失败的原因,但您错过了几个
代码>在代码中…为什么不改为使用$location.url()?仍然得到相同的结果SAM,注入$state并使用$state.go()在状态之间转换,看看是否有效。不知道这是否是失败的原因,但您错过了几个代码>在您的代码中…它应该在没有$timeout
的情况下工作。这是一种不好的做法。它应该在没有$timeout
的情况下工作。这是一种坏习惯。