Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular build not working on server

I am trying to run my AngularJS front-end on server. I am using Yeoman to build the app. I upload the very basic hello world app and I get plain HTML text withou JavaScript loaded. Console in Chrome says this:

Error: Unknown provider: aProvider <- a
    at Error (<anonymous>)
    at http://../scripts/vendor/d10639ae.angular.js:2627:15
    at Object.getService [as get] (http://../scripts/vendor/d10639ae.angular.js:2755:39)
    at http://../scripts/vendor/d10639ae.angular.js:2632:45
    at getService (http://../scripts/vendor/d10639ae.angular.js:2755:39)
    at invoke (http://../scripts/vendor/d10639ae.angular.js:2773:13)
    at Object.instantiate (http://../scripts/vendor/d10639ae.angular.js:2805:23)
    at http://../scripts/vendor/d10639ae.angular.js:4620:24
    at update (http://../scripts/vendor/d10639ae.angular.js:13692:26)
    at http://../scripts/vendor/d10639ae.angular.js:8002:24 d10639ae.angular.js:5526

Anybody experiencing the same and knows the way out?

EDIT:

'use strict';

yoAngApp.controller('MainCtrl',['$scope', '$window', function($scope, $window) {
    $scope.awesomeThings = [
        'HTML5 Boilerplate',
        'AngularJS',
        'Testacular'
    ];

    $scope.records = [{ title : 'one' }, { title : 'two' }, { title : 'three' }, { title : 'four' }];

    $scope.greet = function() {
        ($window.mockWindow || $window).alert('Hello ' + $scope.name);
    }
}]
);
like image 502
Michal Avatar asked Mar 19 '13 10:03

Michal


People also ask

What are the most common build errors in angular?

Many common build errors may occur as a result of rebuilding your app while using ng serve or running ng build or ng build --prod directly. Module Not Found One of the most common Angular errors is the infamous "Module not found" error.

Why is angular-CLI not working?

and run ng serve or ng build. Sorry, something went wrong. The problem is that other programs are competing for inotifies as well (VSCode, etc.) and angular-cli doesn't tell you that you are out, it just silently fails. Sometimes the solution is that you need to close another program that is using up inotifies...

Why is httpclient not working in angular?

Unhandled Promise rejection: No provider for HttpClient! This error tells you, that you have not imported the angular HttpClient Module into your (root) module. To resolve the problem, you need to import the missing module into your module.

Can I manipulate the Dom in angular?

Manipulating the DOM in angular directly is not only considered bad practice. It can also result in your angular app not working in a different environment other than the browser. The most popular example is the so-called Angular Universal project, which enables your angular application to be rendered server-side.


2 Answers

I'm pretty sure that you have used code minifier for production server, am I right?

Anyways, the folks from Angular Js made pretty clear that using minifier can mess up Dependency Injection if it's not done properly. Why this happens? Have a look:

Dependency Injection vs. Code Minifier

function MyController($scope, $log) { ... }

In the snippet above you are making use of implicit DI. Angular sees variable $scope and tries to match it with any managed dependency. In this example it will match it to the $scope object.

This, however, will not work after code minifying, as the result will look somehow like this:

function a(b, c) { ... }

Since variable and function names were minified, Angular cannot know what exactly an "a" is.

Solution

Use explicit Dependency Injection configuration.

var MyController = function($scope, $log) { ... }
MyController.$inject = ['$scope', '$log'];

In this snippet you are defining which dependencies should be resolved by attaching array of their names to special property of controller (or service) called $inject. Now Angular will know that it should resolve $scope and pass it as first parameter to MyController. Then it will resolve $log and pass it as second parameter. It's all possible because minifiers won't modify the contents of string variables.

like image 199
ŁukaszBachman Avatar answered Oct 28 '22 04:10

ŁukaszBachman


As @ŁukaszBachman suggested, you may use $inject annotation or you may use Inline Annotation if you want to:

  1. Keep your dependency annotations close to your function definitions (for better readability).
  2. Stay away from polluting global namespace.

 

app.controller('UsersController',
  [
    '$scope', 'UserService', '$location', '$routeParams',
    function($scope, User, $location, $routeParams) {
      $scope.user = User.get({id: $routeParams.id});
      ...
    }
  ]
);
like image 25
Stewie Avatar answered Oct 28 '22 03:10

Stewie