AngularJS转到状态而不重新加载并且没有值的参数

时间:2018-04-23 18:20:44

标签: angularjs angular-ui-router

在我的应用程序中,我有一个指令,查看$location.search()以决定是否应该显示该指令。 代码看起来有点像这样:

function refresh(stateName) {
    var hideSplash = $location.search().nosplash;

    if (hideSplash === true) {
        model.hide = true;
    } else {
        var templateUrl = getTemplateUrl(stateName);
        if (templateUrl && viewed.indexOf(templateUrl) === -1) {
            viewed.push(templateUrl);
            model.templateUrl = templateUrl;
            model.hide = false;
            model.finished = false;
        }
    }
};

这就像一个魅力。正如您所知,在这种情况下,如果 nosplash 具有值并不重要,则只需预设它即可。 就我而言,路径如下所示:

/相机/结果吗?NOSPLASH

在那条特定的路线上,我把它定义为:

angular.module('widget.wizard')
    .config(pickRoutes);

function pickRoutes($stateProvider) {

    $stateProvider
        .state('home.category.results', configurePickRoute());

    function configurePickRoute() {
        return {
            url: '/results?expanded',
            reloadOnSearch: false,
            views: {
                '': {
                    templateUrl: 'scripts/results/results.html',
                    controller: 'ResultsController',
                    controllerAs: 'controller'
                }
            },
            resolve: {
                bypass: bypassRegistration
            },
            data: {
                pageTitle: 'Your PiiiCKs'
            }
        };
    };

    //////////////////////////////////////////////////

    /* @ngInject */
    function bypassRegistration($location, pkRegisterService) {
        if (!pkRegisterService.options.bypass) // Checks to see if has been set elsewhere
            pkRegisterService.options.bypass = $location.search().bypass;
    };
};

如您所见,我希望它有一个查询参数展开。目前,我的控制器有这个:

self.expanded = $stateParams.expanded;

//////////////////////////////////////////////////

function expand() {
    self.expanded = true;
    $state.go('home.category.results', { expanded: true });
};

在我看来,我有这个HTML:

<div class="panel-more" ng-if="controller.picks">
    <div class="container">
        <div class="row">
            <div class="col-md-12 text-center">
                <button class="btn btn-success" ng-click="controller.expand()" ng-if="!controller.expanded">See your other matches</button>
            </div>
        </div>
    </div>
</div>

<div class="products" ng-if="controller.expanded">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h2>Your other matches</h2>
            </div>
        </div>
    </div>
    <div class="product" pk-product="product" pk-category="controller.category" ng-repeat="product in controller.notPicked"></div>
</div>

正如您所看到的,如果展开属性为true,则隐藏按钮并且&#34;其他匹配&#34;显示。 这确实有效,但网址看起来像这样:

/cameras/results?expanded=true

我希望它看起来像这样:

/cameras/results?expanded

我有办法做到这一点,但仍然有reloadOnSearch: false

1 个答案:

答案 0 :(得分:0)

请试试这个:

网址配置:

url: '/results/:params?'

获取价值:

var params = JSON.parse($stateParams["params"]);
console.log(params.expanded);

转到州:

$state.go('home.category.results', { expanded: true });