0

I am Trying to create dynamic templateUrls with angular component. But i get this error as:

Error: [$injector:unpr] Unknown provider: eProvider <- e

angular
    .module('myApp')
    .component('fieldComponent', {
        templateUrl: function ($element, $attrs) {
           return $attrs.templateUrl || 'some/path/default.html'
        },
        controller:fieldComponentController,
        controllerAs:'vm',
        $routeConfig: [
            {path: '/dashboard', component: 'dashboardApp', name: 'Dashboard', useAsDefault: true}
        ]
    });

As files getting minified, its throwing error as above. So where & how to i inject this dependencies?

2 Answers 2

1

I'm guessing that your DI in the fieldComponentController is getting broken by minification. When the code is minified, your dependencies are getting their names changed to things like 'e', which Angular doesn't know how to handle.

One way to deal with this is by utilizing ng-annotate (https://github.com/olov/ng-annotate) to rewrite those variable names in a minification-safe way.

If you're using a build tool like Webpack to do your bundling and minification, you can simply add the ngAnnotatePlugin to your config and include the string 'ngInject' at the top of your controller definition

angular.module("MyMod").controller("MyCtrl", function($scope, $timeout)        
{
"ngInject";
...
});

ES2015 Version:

export class MyCtrl {
    constructor($scope, $timeout){
        'ngInject';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Its fine to inject the services inside controllers, but how to achieve the same for templateurl function?
Hmmmmm - I'm not sure. I wonder if you could inject those dependencies in the controller and then dynamically set the template by using a div with an ng-include that dynamically loads the template
0

I was able to do it by adding /@ngInject/ at top as suggested by @Matt Richards.

/*@ngInject*/
angular
    .module('myApp')
    .component('fieldComponent', {
        templateUrl: function ($element, $attrs) {
           return $attrs.templateUrl || 'some/path/default.html'
        },
        controller:fieldComponentController,
        controllerAs:'vm',
        $routeConfig: [
            {path: '/dashboard', component: 'dashboardApp', name: 'Dashboard', useAsDefault: true}
        ]
    });

But, finally i ended up with ng-include rather going towards the dynamic template approach, and saved my time covering unit test case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.