2017-04-24 65 views
0

在位置表中添加数据后,单击保存按钮应将其重定向到位置表中的数据列表。但是,它在添加后保持在同一页中。给出相同的路径来修改位置,它工作正常。而添加位置时相同的路径不会重定向。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); 

      }) 
     } 
    }; 

回答

0

如果你想要的角度来了解你的$位置更新,你必须做这样的:

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

如果您正在使用的用户界面的路由器,一个更简洁的方法是使用

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

编辑:

如果你有一个状态变化过程中所发生的错误,您可以通过添加看到它以下代码在您的应用程序宣布您的模块后:

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

感谢您的答案。我正在使用ui-router。我已经尝试了您的建议,但仍然没有重定向到指定的路径。 – Sarah

+0

我通过添加调试路由期间发生的错误的方式更新了我的答案。 你也可以通过添加你声明你想要去的状态来更新你的问题吗? –