Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS goto state without reloading and having a parameter with no value

In my application I have a directive that looks at the $location.search() to decide if the directive should be shown. The code looks a bit like this:

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;
        }
    }
};

This works like a charm. As you can tell, in this case it doesn't matter if nosplash has a value, it just has to be preset. In my case, the path looks like this:

/cameras/results?nosplash

On that particular route, I have it defined as this:

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;
    };
};

As you can see, I want it to have a query parameter expanded. Currently, my controller has this:

self.expanded = $stateParams.expanded;

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

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

and in my view, I have this 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>

As you can see, if the expanded property is true, then the button is hidden and the "Other matches" are shown. This does work, but the url would look like this:

/cameras/results?expanded=true

I would like it to looks like this:

/cameras/results?expanded

Is there a way I can do that, but still have reloadOnSearch: false?

like image 656
r3plica Avatar asked Nov 07 '22 07:11

r3plica


1 Answers

try this please:

URL config:

url: '/results/:params?'

Get value:

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

Go to State:

$state.go('home.category.results', { expanded: true });
like image 136
Sieg Avatar answered Nov 23 '22 22:11

Sieg