“Uncaught Error: [$injector:unpr]” with angular after deployment

后端 未结 7 1236
灰色年华
灰色年华 2020-11-30 21:45

I have a fairly simple Angular application that runs just fine on my dev machine, but is failing with this error message (in the browser console) after I deploy it:

7条回答
  •  天涯浪人
    2020-11-30 22:11

    If you follow your link, it tells you that the error results from the $injector not being able to resolve your dependencies. This is a common issue with angular when the javascript gets minified/uglified/whatever you're doing to it for production.

    The issue is when you have e.g. a controller;

    angular.module("MyApp").controller("MyCtrl", function($scope, $q) {
      // your code
    })
    

    The minification changes $scope and $q into random variables that doesn't tell angular what to inject. The solution is to declare your dependencies like this:

    angular.module("MyApp")
      .controller("MyCtrl", ["$scope", "$q", function($scope, $q) {
      // your code
    }])
    

    That should fix your problem.

    Just to re-iterate, everything I've said is at the link the error message provides to you.

提交回复
热议问题