AngularJS错误:'参数'FirstCtrl“不是一个函数,得到了未定义” [英] AngularJS error: 'argument 'FirstCtrl' is not a function, got undefined'
问题描述
我发现同样的问题被问了几次在这里,我尝试了解决这个问题,但没有什么帮助。
我下面这个教程与理论家的视频。
但是,当我得到控制器的部分和控制器之间的数据共享,我无法得到它的工作。
当我使用Chrome运行它,我得到在控制台这个错误:
参数FirstCtrl不是一个函数,得到了未定义。
块引用>我真的不知道什么是错。在code是相同的从教程。
下面是code为HTML我有:
<!DOCTYPE HTML>
< HTML NG-应用>
< HEAD>
<标题> AngularJS教程:控制器和LT; /标题>
<链接rel =stylesheet属性HREF =mystyle.css>
&所述; SCRIPT SRC =HTTP://$c$c.angularjs.org/1.2.0-rc.2/angular.min.js>&下; /脚本> < /头>
<身体GT; < DIV NG-应用=>
< DIV NG控制器=FirstCtrl> < H1> {{data.message +世界}}< / H1> < DIV CLASS ={{data.message}}>
将我包裹在一个基础组成部分
< / DIV>
< / DIV>
< / DIV> <脚本类型=文/ JavaScript的SRC =main.js>< / SCRIPT> < /身体GT;
< / HTML>这里是code为main.js:
函数FirstCtrl($范围){
$ scope.data = {消息:你好};
}
解决方案您在你的HTML 2无名
NG-应用
指令。结果
失去了一个在您的div。更新结果
让我们尝试不同的方法。结果
定义一个模块中的js文件并分配NG-应用
指令给它。在这之后,确定像纳克组件控制器,而不是作为一个简单的函数:< DIV NG-应用=myAppName>
<! - 或者什么是你的角度应用程序的根节点 - >和JS部分:
angular.module('myAppName',[])
.controller('FirstCtrl',函数($范围){
$ scope.data = {消息:'你好'};
});下面是一个在线演示,这只是这样做: http://jsfiddle.net/FssbL/1/
I noticed the same question was asked a few times here, I tried so solve it but nothing helps.
I'm following this tutorial with the egghead videos.
But when I get at the section of Controllers and Sharing data between controllers, I can't get it to work.
When I run it with Chrome, I get this error in the console:
'argument 'FirstCtrl' is not a function, got undefined'.
I really don't know what's wrong. The code is the same from in the tutorial.
Here is the code for the HTML I have:
<!DOCTYPE html> <html ng-app> <head> <title>AngularJS Tutorials: Controllers</title> <link rel="stylesheet" href="mystyle.css"> <script src="http://code.angularjs.org/1.2.0-rc.2/angular.min.js"></script> </head> <body> <div ng-app=""> <div ng-controller="FirstCtrl"> <h1> {{data.message + " world"}}</h1> <div class="{{data.message}}"> Wrap me in a foundation component </div> </div> </div> <script type="text/javascript" src="main.js"></script> </body> </html>
And here is the code for the main.js:
function FirstCtrl($scope){ $scope.data = {message: "Hello"}; }
解决方案You have 2 unnamed
ng-app
directives in your html.
Lose the one in your div.Update
Let's try a different approach.
Define a module in your js file and assign theng-app
directive to it. After that, define the controller like an ng component, not as a simple function:<div ng-app="myAppName"> <!-- or what's the root node of your angular app -->
and the js part:
angular.module('myAppName', []) .controller('FirstCtrl', function($scope) { $scope.data = {message: 'Hello'}; });
Here's an online demo that is doing just that : http://jsfiddle.net/FssbL/1/
这篇关于AngularJS错误:'参数'FirstCtrl“不是一个函数,得到了未定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!