我正在关注以下视频教程蛋头io http://www.egghead.io/但在尝试效仿他创建工厂时的榜样(参见视频)我不断收到“角度未定义”参考错误,但我已经包含了角度脚本
这是我的 html 页面:
<!DOCTYPE html>
<html>
<head>
<title>Prototype</title>
<link rel="stylesheet" type="text/css" href="foundation.min.css">
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div data-ng-app="">
<div data-ng-controller="FirstController">
<input type="text" data-ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
<div data-ng-controller="SecondController">
<input type="text" data-ng-model="data.message">
<h1>{{ data.message }}</h1>
</div>
</div>
<script type="text/javascript" src="angular.min.js"></script>
</body>
</html>
这是我的 javascript 文件“main.js”:
//Services
// step 1 create an app
var myApp = angular.module('Data', []).
// tep 2 create factory
// Service name, function
myApp.factory('Data', function(){
return { message: "I'm Data from a Service" }
});
//Controllers
function FirstController($scope, Data){
$scope.data = Data;
}
function SecondController($scope){
}
我读过一些发生类似情况的帖子(here https://stackoverflow.com/questions/14260399/angularjs-referenceerror-angular-is-not-defined)如果我错了,请纠正我,但我认为这与引导捆绑有关,并且我已经尝试使用手动引导angular.bootstrap(document, ['Data']);
但没有成功,仍然出现同样的错误。
但我想知道的是,为什么这适用于网上这么多的例子,比如蛋头视频系列,但我有问题,因为我相信我非常密切地关注他的视频。最近版本中的角度有变化吗?
您必须将脚本标记放在引用 Angular 的标记后面。将其移出head
:
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="main.js"></script>
按照您现在设置的方式,您的脚本会在 Angular 加载到页面上之前运行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)