AngularJs script doesnt work after deployment, It gives error [$injector:unpr]

后端 未结 1 1415
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 04:12

I have been building a web project, and time to time I try to publish and see everything looks like it does in localhost. This time, I added angularjs to get/display currenc

相关标签:
1条回答
  • 2020-12-12 05:05

    That might occur if when you deploy your project the JavaScript files are minified and the AngularJS services have not been "injected" properly. If so, try modifying your code like this:

    var currencyCtrl = function($scope, $http) {
      $http.get('http://dummy.com/api/getcurrencyformainscreen').
      success(function(data, status, headers, config) {
        $scope.currencies = data;
      }).
      error(function(data, status, headers, config) {
        //alert(data);
      })
    
    };
    // inject dependencies properly for minification process
    currencyCtrl.$inject['$scope', '$http'];
    
    app.controller("CurrencyController", currencyCtrl);
    

    This is because AngularJS relies on dependency injection. In development mode the parameters ($scope, $http) have the same name and AngularJS injects the dependency (services with the same name) without problems, but in the minified version of the JavaScript file, the name of the parameters are changed randomly, so you must inject them manually with the currencyCtrl.$inject['$scope', '$http']; code.

    0 讨论(0)
提交回复
热议问题