Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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: '/'
            });
    }])

Solution 2:

Make sure you are writing controller only once. write either in ng-controller or in your config route:

app.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/',
        { 
            templateUrl: 'pages/home.html'//Remove controller from here
        });
    }]);

home.html

<!-- Add the ng-controller in your view --><divng-controller="MyItemsController"><!-- Your stuff --></div>

Try using reloadOnSearch: false inside your config it will prevent controller from loading.

Post a Comment for "Controller Getting Called Twice Due To Append Params In Url"