How to pass parameters by Angularjs providers?

Asked

Viewed 2,082 times

2

I have the default site routes on a constant:

app.constant('defaultRoutes', {
    home: {
        url: '/home',
        templateUrl: 'templates/home.html',
        controller: 'HomeController'
    },
   [...demais rotas....]
});

I’m calling this constant in this provider

app.provider("routeConfig", function (defaultRoutes,$httpProvider) {
    [... logica aqui...]
    this.$get = defaultRoutes;
});

This provider will be instantiated in the app.config()

app.config(function($stateProvider,$urlRouterProvider,routeConfigProvider) {
    [...lógica para configurar as rotas...]
})

Until then it is working ok, but the doubt is: how could be made a way to give the option of who to use the class, overwrite the default values without changing the constant?

I’ve tried to replace constant for value but this can only be used after initialization of app.config().

1 answer

0


app.config(['$routeProvider', '$logProvider', function ($routeProvider, $logProvider) {

// Configuração de rotas
$routeProvider
    .when('/page/:ID', {
        templateUrl: 'App/Views/page.html',
        controller: 'Controller'
    })
    .otherwise({
        redirectTo: '/'
    });

}]);

This is the way I use to pass the ID as a parameter on my routes, using angular-route.js, that is called at module startup:

var app = angular.module("app", ["ngRoute"]);
  • 1

    Actually this is not the case, I’m using Angular-Ui-Router to control routes, this explains my receiving app.config ($stateProvider,$urlRouterProvider.. The point is, it’s working like this, but in a static way. I need to make it flexible so that the defaultRoutes may be overwritten

  • But when you use app.Constant, you’re reporting a fixed variable or function or is immutable that is instantiated even before loading the methods. That way there’s no way to be flexible.

  • Just that the question: "What could I use to be flexible?"

  • 1

    Maybe the link can help you: http://odetocode.com/blogs/scott/archive/2013/06/decorating-angularjs-services.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.