部署后AngularJs脚本不起作用,它给出错误[$ injector:unpr] [英] AngularJs script doesnt work after deployment, It gives error [$injector:unpr]

查看:83
本文介绍了部署后AngularJs脚本不起作用,它给出错误[$ injector:unpr]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在构建一个Web项目,有时我会尝试发布并看到一切看起来像在localhost中一样.这次,我添加了angularjs以获取/显示货币并再次部署了该项目.但是,它会向浏览器中的用户显示{{currencies}}.

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 currency and deployed the project again. But, it shows {{currencies}} to users in browser.

Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=nProvider%20%3C-%20n%20%3C-%20CurrencyController

我的angularjs代码看起来像这样...

My angularjs code looks like this...

app.controller("CurrencyController", 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);
                })

});

我在做什么错了?

推荐答案

如果在部署项目时将JavaScript文件缩小并且未正确注入" AngularJS服务,则可能会发生这种情况.如果是这样,请尝试按以下方式修改您的代码:

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);

这是因为AngularJS依赖于依赖项注入.在开发模式下,参数($scope$http)具有相同的名称,并且AngularJS注入依赖项(具有相同名称的服务)没有问题,但是在JavaScript文件的缩小版本中,参数的名称已更改是随机的,因此您必须使用currencyCtrl.$inject['$scope', '$http'];代码手动注入.

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.

这篇关于部署后AngularJs脚本不起作用,它给出错误[$ injector:unpr]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆