javascript 为什么这个 Angular 控制器会抛出“错误:未知提供者:nProvider <- n”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15720580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does this Angular controller throw "Error: Unknown provider: nProvider <- n"?
提问by Chaddeus
<div ng-app="">
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.message" />
{{data.message + " world"}}
</div>
</div>
function FirstCtrl($scope) {
$scope.data = {
message: "Hello"
};
}
I am just starting to learn Angular using the videos on Egghead.io. Following along I got stuck on the 2nd video where John discusses controllers. It works in his video, fails on my machine.
我刚刚开始使用 Egghead.io 上的视频学习 Angular。接下来我被困在约翰讨论控制器的第二个视频中。它在他的视频中有效,在我的机器上失败。
the code is so basic I can't figure out what's throwing this error:
代码太基本了,我无法弄清楚是什么引发了这个错误:
> Error: Unknown provider: nProvider <- n
> at Error (<anonymous>)
> at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:29:36
> at Object.c [as get] (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:26:310)
> at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:29:121
> at c (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:26:310)
> at d (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:26:444)
> at Object.instantiate (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:28:80)
> at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:51:512
> at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:43:67
> at n (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.3/angular.min.js:7:43)
This error gets thrown if I use the google CDN as well (from the error, I thought perhaps it was the cdn's fault).
如果我也使用 google CDN,则会抛出此错误(从错误中,我认为这可能是 CDN 的错)。
回答by Chaddeus
Turns out the problem was that my scripts were being minified, and the minifier was changing the name of the $scope variable. The Angular.js docs do mention how to minify Angular code.
原来问题是我的脚本被缩小了,并且缩小器正在更改 $scope 变量的名称。Angular.js 文档确实提到了如何缩小 Angular 代码。
回答by Ryan O'Neill
As far as your fiddle goes, if you change the AngularJS usage from 'onLoad' to 'No wrap - in '<head>' or 'No wrap - in <body>' it will work fine.
就您的小提琴而言,如果您将 AngularJS 的用法从“onLoad”更改为“No wrap - in '<head>' 或 'No wrap - in <body>',它将正常工作。
回答by charlietfl
Only problem in fiddle demo is setting for code to run within load handler.
小提琴演示中的唯一问题是设置在加载处理程序中运行的代码。
Anguar can't find your controller function when it tries to parse the DOM
Anguar 在尝试解析 DOM 时找不到您的控制器函数
working version , simply change onload
in top left
工作版本,只需更改onload
左上角