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

查看:28
本文介绍了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天全站免登陆