angularjs中的重定向错误
在位置表中添加数据后,单击“保存”按钮应将其重定向到位置表中的数据列表。但是,添加后它仍保持在同一页面中。修改位置的路径相同,可以正常工作。而在添加位置时,相同的路径不会重定向angularjs中的重定向错误,angularjs,Angularjs,在位置表中添加数据后,单击“保存”按钮应将其重定向到位置表中的数据列表。但是,添加后它仍保持在同一页面中。修改位置的路径相同,可以正常工作。而在添加位置时,相同的路径不会重定向 function locationController($scope, $state, $rootScope, locationServices,$location, locations, location, primaryLocation, $stateParams,locationTypes, countries)
function locationController($scope, $state, $rootScope, locationServices,$location, locations, location, primaryLocation, $stateParams,locationTypes, countries) {
var vm = this;
$scope.locations = locations.data;
$scope.location = location.data;
if (primaryLocation.data && primaryLocation.data[0])
$scope.primaryLocation = primaryLocation.data[0];
if (!$scope.location) {
var location = {};
if ($stateParams.accountId) {
$scope.location = {accountId: $stateParams.accountId };
} else {
$scope.location = location;
}
}
$rootScope.title = "Locations";
$scope.locationslist = "views/locations.html";
$scope.addOrModifyLocation = function (location) {
if (location._id) {
locationServices.modifyLocation(location).then(function (response) {
$location.path('/account/locations/contacts/' + location.accountId + '/' +location.accountId);
// $state.reload();
})
} else {
location.status = 'ACTIVE';
locationServices.addLocation(location).then(function (response) {
$location.path('/account/locations/contacts/' + location.accountId + '/' +location.accountId);
})
}
};
如果你想知道你的$location更新,你必须这样做:
$rootScope.$apply(function() {
$location.path("/my-path"); // path must start with leading /
});
如果您使用的是ui路由器,则更干净的方法是使用
$state.go('stateName', {'accountId' : location.accountId, });
编辑:
如果在状态更改期间发生错误,您可以在声明模块后在应用程序中添加以下代码来查看错误:
angular.module("appName").run([
"$rootScope",
function ($rootScope) {
$rootScope.$on("$stateChangeError", function(error) {
console.log(error);
});
}
]);
谢谢你的回答。我正在使用ui路由器。我尝试了你的建议,但仍然没有重定向到指定的路径。我通过添加调试路由过程中发生的错误的方法更新了我的答案。您还可以通过添加您声明要进入的状态的方式来更新您的问题吗?