angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.54k stars 3k forks source link

angularJS ui-router function work with $state.go and not with $location.path #3584

Closed wiemKh closed 6 years ago

wiemKh commented 6 years ago

I'm new to AnguLarJs , i'm using ui router , i have a function that work well with state.go() but i can't figure out why this does'nt work with $location.path()

this is my config

.state('customer.customerDetail', {
        url: '/customerdetail',
        templateUrl: 'views/customerDetail.html',
        controller: 'customerDetailCtrl',
        data: {
            pageTitle: 'Customer detail'
        },
        params: {
            selectedCustomer: null,

        },
        resolve: {
            loginRequired: loginRequired,
            loadPlugin: function ($ocLazyLoad) {
                return $ocLazyLoad.load([{
                        files: ['js/plugins/hammer/hammer.min.js']
                    },
                    {
                        files: ['css/customerDetail.css'],
                        cache: false,
                    },
                ]);
            }
        }
    })

i have this function that worked well with $state.go()

$scope.goToCustomerDetail = function (customer) {
    $sessionStorage.setObject('customer', customer);
    $rootScope.$emit("gocustomer", customer);
    $state.go('customer.customerDetail', {
        selectedCustomer: customer
    });
}

but when i change it with $location doesnt work anymore

$scope.goToCustomerDetail = function (customer) {
    $sessionStorage.setObject('customer', customer);
    $rootScope.$emit("gocustomer", customer);
     $location.path('/customer/customerdetail').search({ selectedCustomer : customer }); 
}

what i'm doing wrong? Thanks :)

christopherthielen commented 6 years ago

You forgot to put the parameter in your URL.

        url: '/customerdetail?selectedCustomer',

Ui-Router supports parameters that are not reflected in the URL. Your parameter is declared in the parameters block, but doesn't appear in the URL.