gpt4 book ai didi

javascript - AngularJS,正确的引用,但 "ReferenceError: angular is not defined"

转载 作者:行者123 更新时间:2023-11-28 19:41:48 28 4
gpt4 key购买 nike

我正在尝试创建一个简单的 AngularJS 代码,它接受一个 JSON 对象并将其打印在 HTML 表中。

我正在使用这个Fiddle中的代码(完美运行): http://jsfiddle.net/mjaric/pJ5BR/

但我无法在单个 HTML 文件(本地)中重新创建代码,而不会遇到错误。

编辑:

ReferenceError: angular is not defined

在线错误(在脚本中):

var app = angular.module('myApp', []);

我尝试通过 Google 以及本地引用 AngularJS 和 JQuery。完全没有区别。

这是我的 html 文件的头部:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

脚本片段:

 <script>

var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
{
id: 1,
firstName: "Peter",
lastName: "Jhons"},
{
id: 2,
firstName: "David",
lastName: "Bowie"}
]));


var app = angular.module('myApp', []);

function PeopleCtrl($scope, $http) {

$scope.people = [];

$scope.loadPeople = function() {
var httpRequest = $http({
method: 'POST',
url: '/echo/json/',
data: mockDataForThisTest

}).success(function(data, status) {
$scope.people = data;
});

};

}

</script>

最后是正文:

  <div ng-app="myApp">
<div ng-controller="PeopleCtrl">
<p> Click <a ng-click="loadPeople()">here</a> to load data.</p>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="person in people">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
</tr>
</table>
</div>
</div>

谢谢!

最佳答案

您正在使用方案相对 url 作为对 Angular 的引用:

src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"

这使用与您访问 HTML 文档相同的方案,如果您使用 http://https:// 加载 HTML 文档,它将完美地工作.

您的本地文件可能是从文件系统(使用 file:// URI)而不是本地网站(使用 http://localhost/),并且该资源在 file://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js 处不可用。

您可以使用以下方法快速破解此问题

src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"

但是您最好使用本地网络服务器进行测试,因为有很多东西(尤其是您正在使用的 Ajax 周围)在 file:// 之间工作方式不同http://.

关于javascript - AngularJS,正确的引用,但 "ReferenceError: angular is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24910653/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com