$ location.path相同的网址不起作用

时间:2015-09-04 12:53:54

标签: javascript jquery angularjs redirect

以下是我的代码:

app.controller('lookupMasterController', [
    '$scope', '$routeParams', '$location', '$modal', 'lookupService', function ($scope, $routeParams, $location, $modal, lookupService) {

$scope.openCreatePopup = function () {

        var modalInstance = $modal.open({
            animation: true,
            templateUrl: 'app/popups/add-lookup.html',
            controller: function ($scope, $modalInstance, $location, $routeParams, lookupService) {
                $scope.cancel = function () {
                    $modalInstance.close();
                }

                $scope.addLookup = function () {
                    $scope.lookup.lookUpConfigId = $routeParams.lookupMasterId;
                    lookupService.save($scope.lookup).$promise.then(
                        function (value) {
                            $location.path('/edit-lookup/' + $routeParams.lookupMasterId);
                        $scope.$apply();// Even this is not working.
                        // Tried the below as well:
                        //$scope.$apply(function () {
                          //      $location.path('/edit-lookup/' + //$routeParams.lookupMasterId);
                           // });

                            $modalInstance.close();
                        },
                    function (error) { /*Do something with error*/
                        alert(error.message);
                    });
                }
            },
            size: 'lg'
        });
    }
}
]);

我打开Popup以添加新查找,然后重新加载页面以查看更改。但问题是:$location.path('url');无效。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该使用$state服务。尝试

$state.go('/edit-lookup/' + $routeParams.lookupMasterId);

不要忘记在你的控制器启动中注入$state。在您的模块定义中,您需要通过&u;作为依赖:

E.g. angular.module('my_app', ['ui.router'])

修改

我认为您没有在网页中加载ui-router.min。请在angular.js

之后加载
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>