AngularJS 错误:'参数'FirstCtrl' 不是函数,未定义' [英] AngularJS error: 'argument 'FirstCtrl' is not a function, got undefined'
问题描述
我注意到同样的问题在这里被问了几次,我尝试解决它但没有任何帮助.
我正在关注本教程的蛋头视频.
但是当我进入控制器和在控制器之间共享数据部分时,我无法让它工作.
当我使用 Chrome 运行它时,我在控制台中收到此错误:
<块引用>'参数'FirstCtrl' 不是函数,未定义'.
我真的不知道怎么了.代码与教程中的相同.
HTML
<头><title>AngularJS 教程:控制器</title><link rel="stylesheet" href="mystyle.css"><script src="http://code.angularjs.org/1.2.0-rc.2/angular.min.js"></script>头部><身体><div ng-app=""><div ng-controller="FirstCtrl"><h1>{{data.message + "world"}}</h1><div class="{{data.message}}">将我包裹在基础组件中
<script type="text/javascript" src="main.js"></script></html>
main.js
function FirstCtrl($scope){$scope.data = { 消息:你好" };}
您的 html 中有 2 个未命名的 ng-app
指令.
丢失 div 中的那个.
更新
让我们尝试不同的方法.
在您的 js 文件中定义一个模块并将 ng-app
指令分配给它.之后,将控制器定义为一个 ng 组件,而不是一个简单的函数:
<!-- 或者你的 angular 应用程序的根节点是什么 -->和js部分:
angular.module('myAppName', []).controller('FirstCtrl', function($scope) {$scope.data = {message: '你好'};});
这是一个在线演示: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.
HTML
<!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>
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 the ng-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屋!
查看全文