gpt4 book ai didi

javascript - ng-view 不显示路线(angularjs)

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

我正在做有关 angularjs 的教程。在使用路线之前一切都很好。我之前搜索过这个问题,但它对我不起作用。
我正在执行作者输入的代码,但它不起作用。ng-view 放入index.html

<html ng-app="githubViewer">
<head>
<title>Demo</title>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/angular-route.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="MainController.js"></script>
<script type="text/javascript" src="github.js"></script>
</head>
<body>
<h1>Github Viewer</h1>
<div ng-view=""></div>
</body>

应用程序.js

(function() {
var app = angular.module("githubViewer", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/main", {
templateUrl: "main.html",
controller: "MainController"
})
.otherwise({redirectTo:"/main"});
});})();

MainController.js

(function() {
var app = angular.module("githubViewer");
var MainController = function(
$scope, $interval, $location) {
var decrementCountdown = function() {
$scope.countdown -= 1;
if ($scope.countdown < 1) {
$scope.search($scope.username);
}
};
var countdownInterval = null;
var startCountdown = function() {
countdownInterval = $interval(decrementCountdown, 1000, 5, $scope.countdown);
};
$scope.search = function(username) {
if (countdownInterval) {
$interval.cancel(countdownInterval);
$scope.countdown = null;
}
};
$scope.username = "angular";
$scope.countdown = 5;
startCountdown();
};
app.controller("MainController", MainController);})();
<小时/>

main.html

<div>
{{countdown}}
{{username}}
<form name="searchUser" ng-submit="search(username)">
<input type="search" required placeholder="usẻname to ind" ng-model="username" />
<input type="submit" value="Search" ng-click="search(username)">
</form>

github.js

(function() {

var github = function($http) {

var getUser = function(username){
return $http.get("https://api.github.com/users/" + username)
.then(function(response){
return response.data;
});
};

var getRepos = function(user){
return $http.get(user.repos_url)
.then(function(response){
return response.data;
});
};

return{
getUser : getUser,
getRepos: getRepos
};
};

var module = angular.module("githubViewer");
module.factory("github", github);})();

最佳答案

您的代码中有错误:

var MainController = function($scope, $interval, , $location) {
// unnecessary comma here ----^

删除逗号(或插入缺少的参数),您的应用程序应该开始运行。

一般来说,我建议在编码期间始终保持开发者控制台打开。

关于javascript - ng-view 不显示路线(angularjs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753400/

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