Search code examples
angularjsngroute

$routeProvider route params from database


I want to load fill the route params with data from database.

For example, I have

$routeProvider.when('/:param1/:param2', loadParams())

And loadParams function

function loadParams() {
    var data = {
        path: window.location.pathname
    };
    $.ajax({
        type: 'POST',
        url: 'getRouteParams',
        data: data,
        success: function(data, status) {
            var attributes = {};
            attributes.controller = data.controller;
            attributes.templateUrl = data.templateUrl;
            return attributes;
        }
    });
}

But this function give me an error

Cannot read property 'reloadOnSearch' of undefined

Solution

  • I answer myself

    The solution is deactivate the async and put the return out of the "success"

    function loadParams() {
        var data = {
            path: window.location.pathname
        };
        var attributes = {};
        $.ajax({
            type: 'POST',
            url: 'getRouteParams',
            data: data,
            async: false,
            success: function(data, status) {
                attributes.controller = data.controller;
                attributes.templateUrl = data.templateUrl;
            }
        });
        return attributes;
    }