0
votes

In my AngularJS app I have one template and one controller that I can reach via 2 different url:

$routeProvider.when('/site/contents/:obj/:parent/:isnew?', {
    templateUrl: '/partials/site/data.html',
    resolve: {
        loggedin: checkLoggedin
    }
});

$routeProvider.when('/site/contents/new-obj', {
    templateUrl: '/partials/site/data.html',
    resolve: {
        loggedin: checkLoggedin
    }
});

The second route should statically set a parameter isnew=1, as I would do using a RewriteRule in apache, but looking at the documentation https://docs.angularjs.org/api/ngRoute/provider/$routeProvider it seems impossible to do.

Anyone had encountered the same question?

1

1 Answers

0
votes

You can't really set default route parameters in Angular, if that is what you are trying to do.

The best idea would probably be to check in your controller if $routeParams.isnew is set and, if not, default it to 1.

Angular routes are supposed to be fairly simple rather than being heavily pattern based like RewriteEngine in apache, so the logic should be in your controller.

Your controller could look like so:

function($routeParams) {
    $scope.isNew = ($routeParams.isnew || 1);
}

In your case, you can probably even get away with having no isnew param. Use the first route for modifying existing objects and the second for creating them (so the existence of obj in the params defines if it is new or not).