angularjs中的重定向错误

时间:2017-04-24 09:55:11

标签: angularjs

在位置表中添加数据后,单击“保存”按钮会将其重定向到位置表中的数据列表。但是,它在添加后会保留在同一页面中。修改位置的路径相同,工作正常。而添加位置时,相同的路径不会重定向。

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);

            })
        }
    };

1 个答案:

答案 0 :(得分:0)

如果你想让angular知道你的$ location更新,你必须这样做:

  $rootScope.$apply(function() {
    $location.path("/my-path");   // path must start with leading /
  });

如果你正在使用ui-router,那么更简洁的方法就是使用

$state.go('stateName', {'accountId' : location.accountId, });

编辑:

如果在状态更改期间发生错误,您可以在声明模块后在应用中添加以下代码来查看:

angular.module("appName").run([
    "$rootScope",
    function ($rootScope) {
        $rootScope.$on("$stateChangeError", function(error) {
            console.log(error);
        });
    }
]);