gpt4 book ai didi

javascript - AngularJS 路线

转载 作者:行者123 更新时间:2023-11-27 23:55:02 29 4
gpt4 key购买 nike

我是 AngularJS 的新手。我正在尝试从这个网站( https://docs.angularjs.org/tutorial/step_07 )学习 AngularJS。我的代码如下

index.html

<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
<meta charset="utf-8">
<title>Angular Project</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/app.css">
<script src="js/angular.min.js"></script>
<script src="js/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

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

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', function ($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'
});
}]);

我的代码位于我计算机的 htdocsangu 文件夹内。我正在尝试浏览以下网址

http://localhost/angu/#/phones 

但是我得到了白色的空白页。谁能说一下问题出在哪里吗?

最佳答案

你的controller.js应该有phonecatControllers模块名称而不是phonecatApp,所以目前发生的事情是你通过声明刷新phonecatApp模块再次在 controller.js 中。

从技术上讲,您应该在那里使用 phonecatControllers 名称,并且您的控制台必须显示错误 $injector moduler 错误,因为您尚未声明 phonecatControllers 模块已注入(inject)到 phonecatApp 模块中。

Controller.js

var phonecatControllers = angular.module('phonecatControllers', []); //<=-change here

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', function ($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;
}]);

关于javascript - AngularJS 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341124/

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