【问题标题】:$location.path same url not working$location.path 相同的网址不起作用
【发布时间】:2015-09-04 12:53:54
【问题描述】:

下面是我的代码:

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'
        });
    }
}
]);

我正在打开一个弹出窗口以添加新查找,然后重新加载页面以查看更改。但问题是:$location.path('url'); 不工作。

【问题讨论】:

标签: javascript jquery angularjs redirect


【解决方案1】:

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

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

别忘了在你的控制器初始化中注入$state。另外,在您的模块定义中,您需要将“ui.router”作为依赖项传递:

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>

【讨论】:

  • 这将重新加载整个页面,这不是单页应用程序中想要的。所以我不想尝试这个。
  • 错误:未知提供者:$stateProvider
  • 我没有在我的应用程序中使用状态。现在它给出了这个错误:Could not resolve '/edit-lookup/7' from state ''
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多