【问题标题】:$location.search() setter not causing reload in ui-router$location.search() 设置器不会导致在 ui-router 中重新加载
【发布时间】:2015-03-20 22:10:28
【问题描述】:

使用 ngRoute,使用 $location.search() 作为设置器将导致路由重新加载,您可以使用 reloadOnSearch: false 关闭此行为。

但是,如果我将 ngRoute 代码与上面的 ui-route 代码交换,控制器将不再重新加载。

如何在 ui-router 中触发路由重新加载同时将所有查询参数保留在 url 中,如 ngRoute?

编辑: 我需要将查询保留在 url 中,并且在路由重新加载后保持不变,因为我希望用户能够为其添加书签。

/?people=bob&people=james&people=pete...&color=red&color=blue...etc

script.js

angular.module('app', ['ui.router', 'ngRoute'])

// .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
//     $stateProvider
//         .state('home', {
//             url: '/',
//             templateUrl: 'page1.html',
//             controller: 'MyController'
//         });

//         $urlRouterProvider.otherwise('/');
// }])

.config(function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'page1.html',
            controller: 'MyController'
            // reloadOnSearch: false
        });
})

.controller('MyController', ['$scope', '$location',
    function($scope, $location) {
        console.log('reloaded');
        $scope.changeQuery = function(testId) {
            $location.search('id', testId);
            console.log($location.absUrl());
        };
    }
]);

index.html

<!DOCTYPE html>
<html lang="en" ng-app="app">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.13/angular-ui-router.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.js"></script>
    <script src="script.js"></script>
</head>

<body ng-controller="MyController">
    <h3>ui router</h3>
    <ui-view></ui-view>

    <h3>ng router</h3>
    <ng-view></ng-view>

</body>

</html>

page1.html

<h1>Change query example</h1>
<input ng-model="testId" />
<button ng-click="changeQuery(testId)">Change!</button>

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    在您的控制器中,不要直接更改 $location,而是尝试使用 $state.go(...) 更改 $state

    在此处记录: http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state

    angular.module('app', ['ui.router', 'ngRoute']).config(['$stateProvider','$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider.state('home', {
            url: '/?key=:id',
            templateUrl: 'page1.html',
            controller: 'MyController'
        });
    $urlRouterProvider.otherwise('/');
    }]).controller('MyController', ['$scope', '$state', '$location',
    function($scope, $state, $location) {
        console.log('reloaded');
        $scope.reload = function() {
          $state.reload();
        };
    
        $scope.changeQuery = function(testId) {
            $state.go('home', {id: testId});
            console.log($location.absUrl());
        };
    }
    ]);
    

    示例: plunker

    【讨论】:

    • 我需要将查询保留在 url 中,并且在路由重新加载后保持不变,因为我希望用户能够为其添加书签。示例网址:/?people=bob&people=james&people=pete...&color=red&color=blue...etc
    • 在这种情况下,只需使用 $state.reload()
    • 非常感谢。通过将 ?key=:id 添加到 state url,location.search() 现在会触发路由重新加载
    【解决方案2】:

    保留对旧查询的引用,然后在您想要更改查询时扩展它并重新加载

    var oldQuery = $location.search(); // save the current query
    $scope.changeQuery = function(newQuery) {
      //whenever a new query comes, extend it and load the url again
      angular.extend(newQuery, oldQuery);
      $location.search(newQuery);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多