gpt4 book ai didi

javascript - Angularjs教程第7步;无法看到动态创建的列表

转载 作者:行者123 更新时间:2023-11-28 01:52:26 25 4
gpt4 key购买 nike

您好,我正在关注 Angularjs 教程,直到第 7 步,教程就不再适合我了。我的index.html 变成了空白,我不知道为什么。

这是代码
索引.html

<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
<meta charset="utf-8">
<title>Google Phone Gallery</title>
<link rel="stylesheet" href="css/app.css">
<!--<link rel="stylesheet" href="css/bootstrap.css">-->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
<script src="lib/angular/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>

</head>
<body >
<div ng-view></div>

</body>
</html>

Controller .js

 'use strict';

/* Controllers */

var phonecatApp = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http',
function PhoneListCtrl($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});
$scope.orderProp = 'age';
}]);

phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.phoneId = $routeParams.phoneId;
}]);

app.js

 'use strict';

/* App Module */

var phonecatApp = angular.module('phonecatApp', [
'ngRoute',
'phonecatControllers'
]);

phonecatApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
when('/phones/:phoneId', {
templateUrl: 'partials/phone-detail.html',
controller: 'PhoneDetailCtrl'
}).
otherwise({
redirectTo: '/phones'
});
}]);

电话列表.html

<div class="container-fluid">
<div class="row-fluid">
<div class="span2">
<!--Sidebar content-->

Search: <input ng-model="query">
Sort by:
<select ng-model="orderProp">
<option value="name">Alphabetical</option>
<option value="age">Newest</option>
</select>

</div>
<div class="span10">
<!--Body content-->

<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp" class="thumbnail">
<a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"> </a>
<a href="#/phones/{{phone.id}}">{{phone.name}}</a>
<p>{{phone.snippet}}</p>
</li>
</ul>

</div>
</div>
</div>

这也是我得到但不明白的控制台错误

  GET file:///C:/Users/John/Desktop/Angular-Family/app/lib/angular/angular-  route.js  index.html:9
Uncaught ReferenceError: phonecatControllers is not defined controllers.js:7
Uncaught Error: No module: ngRoute

有什么想法吗?

编辑2:

  Failed to load resource file:///C:/Users/John/Desktop/Angular-Family/app/app.js
Failed to load resource file:///C:/Users/John/Desktop/Angular- Family/app/controllers.js
Uncaught Error: [$injector:modulerr] Failed to instantiate module phonecatApp due to:
Error: [$injector:nomod] Module 'phonecatApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.0-rc.2/$injector/nomod?p0=phonecatApp
at http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:78:12
at http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:1330:36
at ensure (http://angular.github.io/angular-phonecat/step- 7/app/lib/angular/angular.js:1268:38)
at module (http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:1328:14)
at http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:3071:26
at Array.forEach (native)
at forEach (http://angular.github.io/angular-phonecat/step- 7/app/lib/angular/angular.js:224:11)
at loadModules (http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:3065:5)
at createInjector (http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:3007:11)
at doBootstrap (http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js:1152:20)
http://errors.angularjs.org/1.2.0-rc.2/$injector/modulerr?p0=phonecatApp&p1…2Fangular- phonecat%2Fstep-7%2Fapp%2Flib%2Fangular%2Fangular.js%3A1152%3A20) angular.js:3097

最佳答案

您正在 phonecatControllers 模块上定义 Controller 。但是该模块尚未定义。因此,当 Angular 应用程序尝试注入(inject)模块时,它无法找到它。

尝试更改以下行。

var phonecatApp = angular.module('phonecatControllers', []);

到此

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

除此之外,Angular 也无法加载 ngRoute 依赖项。脚本引用似乎存在。您可能需要检查并确保 lib/angular/angular-route.js 没有返回 404。

更新**

我认为现在的问题是 Angular 和 Angular-Route 的版本不匹配。路线提供商正在寻找的服务不可用。

我能够使用教程演示中的相同脚本让它工作。

Working Plunker Example

<script src="http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular.js"></script>
<script src="http://angular.github.io/angular-phonecat/step-7/app/lib/angular/angular-route.js"></script>

这绝不是一个理想的解决方案,但应该可以帮助您继续学习本教程。您当然可以将这些文件的内容下载到本地计算机。

关于javascript - Angularjs教程第7步;无法看到动态创建的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19547764/

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