Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

controller getting called twice due to append params in url

I am in angularjs. I am trying to append paramter in url using $location.search('sid', key);. The value of key is coming from another server by http request. Here is the code for append parameter.

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller'
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])
.run(['$route', '$rootScope', '$location' ,'getAuthDetails', function($route, $rootScope, $location,getAuthDetails) {
        var key;

        getAuthDetails.get().then(function(data){
            key = data.key;
            $rootScope.query = 'sid='+key;
            console.log($location.$$path);
            $location.search('sid', key);  //append parameter. This is calling controller twice
        });

        $rootScope.$on('$routeChangeSuccess', function () {
            $rootScope.query = 'sid='+key;
        });
    }]);

The problem i am having is . As the sid get append in url. The console.log i put in test1Controller getting called twice. Sid is used to connect with socket connection.

Is there any workaround to call the controller after the url append.

like image 510
NeiL Avatar asked Dec 02 '25 06:12

NeiL


1 Answers

You can use reloadOnSearch: false

Example:

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller',
                reloadOnSearch: false // wont reload the controller when the search query changes
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])
like image 87
Kushal Avatar answered Dec 03 '25 19:12

Kushal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!