javascript - Extending a service provider (provider) -
i'm trying extend service provider; more $routeprovider ngroute module
my extended provider looks this:
angular .module('myapp') .provider('customroute', customroute) function customroute($routeprovider) { var extendedprovider = angular.extend({}, $routeprovider, { // customization here }); // these print identical objects console.log(extendedprovider); console.log($routeprovider); this.$get = function() { return extendedprovider; } }
the config of routes looks this:
angular .module('myapp') .config(routes); function routes(customrouteprovider, $routeprovider) { /* print object looks same ones printed in provider definition */ console.log($routeprovider); /* object not $routeprovider more :( */ console.log(customrouteprovider); customrouteprovider .when('/', { templateurl: 'path/to/some/template.html', controller: 'somecontroller', }) .otherwise({ redirectto: '/' }); }
i've read thread:
but talk extending "factory"-service
can explain happening here? appreciated.
in order extend $routeprovider
customroute
function must return extended provider object:
function customroute($routeprovider) { var extendedprovider = angular.extend({}, $routeprovider, { // customization here mergeroutes: function() {} }); return extendedprovider; }
after example new custom method customrouteprovider.mergeroutes
:
customrouteprovider .when('/', { templateurl: 'path/to/some/template.html', controller: 'somecontroller', }) .otherwise({ redirectto: '/' }) .mergeroutes({ ... });
demo play: http://plnkr.co/edit/mht94rflscz2jhcwpcai?p=preview
for purposes don't need mess this.$get
, need add new methods $routeprovider
. this.$get
returns service object instance/constructor corresponding provider service ($route
in case of $routeprovider
).
Comments
Post a Comment