Angularjs templateUrl error does not find html file

Asked

Viewed 354 times

2

PROBLEM

I have a problem with the minification of my project when it calls the templateUrl on the route, I am using Grunt to generate my build. In my development environment it works correctly, only has this error in my build generated.

Research

I searched to see if it was no DI problem but it’s all right.

CODE

app js.

angular
      .module('myModule', [
        'ngResource',
        'ngRoute',
        'myModule.agenda',
        'myModule.agenda.controller',
        'myModule.agenda.services',
        'myModule.agenda.filters'
      ])
      .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'app/scripts/agenda/views/welcome.html',
                controller: 'mainCtrl'
            })
      });

Folders

  • App
    • scripts
      • agenda
        • views
          • main.html
      • html app.
  • You are using Uglify to minify?

  • That same Lucas

  • By chance your local application runs on root, and the client runs on a subdirectory?

  • Runs only local at the moment and yes root.

3 answers

2

  • Good morning Lucas, I entered this option in my Gruntfile.js but the same thing happens.

  • The link says to rotate the ng-min before turning the uglify solves the problem too.

  • Whoa, I tried too hard and on... my Gruntfile.js to make it easy.Gruntfile.js

0

DEDUCTION OF THE PROBLEM

The error occurred because it was not carrying the file. Together, Grunt made the script folder only try to concatenate the JS files and take the final file and the remaining (that would be my html) would not go in the build.

SOLUTION

My solution was to create a folder a directory before the scripts (app root) called views, where I follow the order of the modules created in the script folder.

Thus remaining: /app/views/agenda/index.html

EXTRA

I’m sure there must be other possible solutions, even in the Grantfile.js file, if you have any solutions, I will be interested to see. Thank you all for your help!

  • 1

    I just found a similar solution, it says to do templateUrl: '../app/scripts/agenda/views/welcome.html' because when Voce minifica the HTML it changes place, then relative links end up getting lost. http://stackoverflow.com/questions/22718455/using-usemin-within-angularjs-partials

  • Interesting I’ll try.

  • got something?

  • Same problem did not occur...

0


Understanding the problem

Angularjs Injects service dependency according to argument name when you do not specify this.

In this case, the argument $routeProvider is being minified and therefore prevented from being injected. So probably the error you are logging in is undefined is not a function.

The solution

You must call the config passing not straight to function, but a array where the values are the dependencies and the last position of the array is the Function that will use it:

Example:

.config(['$dependencia1', '$dependencia2', function($dependencia1,$dependencia2){} ]);

That way, the minification would not cause problems, because it keeps the strings in the original states. The minified file would look like this:

.config(['$dependencia1','$dependencia2',function(a,b){}]);

Where a received the injection of $dependencia1 and b received the injection of $dependencia2. If you didn’t have this array, Angular would search for registered services wherever their names were a and b

Copy 'n' Paste

One should only say explicitly which is the injection of $routeProvider specifying this in the dependency array that goes to the config:

angular
      .module('myModule', [
        'ngResource',
        'ngRoute',
        'myModule.agenda',
        'myModule.agenda.controller',
        'myModule.agenda.services',
        'myModule.agenda.filters'
      ])
      .config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'app/scripts/agenda/views/welcome.html',
                controller: 'mainCtrl'
            });
      }]);
  • And oh Anisanwesley, so I made the change but the same problem occurs. When I put the dependency this way you suggest anywhere the error but if I put it that way .$inject = ['$Scope it works in other dependendices, but my ** not found ** problem is still there.

Browser other questions tagged

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