gpt4 book ai didi

javascript - 我的 AngularJS 路线出了什么问题?

转载 作者:行者123 更新时间:2023-12-01 03:01:16 27 4
gpt4 key购买 nike

可以这么说,我创建了一个 AngularJS 沙箱。我使用 Bootstrap 提供的模板 html 来提高设置速度,并且我计划使用 Bootstrap 来快速实现它。在设置一切时,我注意到我的路线无法正常工作。在我的index.html文件,我尝试将我的 Angular 文件放在几个地方,但我不认为这是问题所在。我也尝试过使用 <ng-view></ng-view> ,而不是使用 div 。在 app.js ,我尝试将 templateUrl 的路径从根目录重命名为最终文件 app/home/home.html ,以及许多其他配置。当我在浏览器中加载时,我得到的只是导航菜单。我什至没有从 templateUrl 获取 html。我可能对这一切都是错的,但我无法弄清楚。我已在我的 index.html 中放置了通用 Bootstrap 导航我的初始设置文件。我的问题可能是什么?

index.html

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular-route.js"></script>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
</head>
<body>

<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<a class="navbar-brand" href="home.html">Angular</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link" href="#">Home</a>
<a class="nav-item nav-link" href="#">About</a>
<a class="nav-item nav-link" href="#">Contact</a>
</div>
</div>
</nav>



<ng-view>
</ng-view>



<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="app/app.js"></script>
<script src="app/home/HomeController.js"></script>
</body>
</html>

app.js

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

app.config(function($routeProvider){
$routeProvider
.when("/home", {
templateURL: "home.html",
controller: "HomeController"
})
.when("/about", {
templateURL: "about.html",
controller: "AboutController"
})
.otherwise({redirectTo: "/home"});
});

HomeController.js

var app = angular.module('myApp');

app.controller('HomeController', function($scope){

$scope.noun = {
person: "Maximus Aurelius",
place: "World",
thing: "sword"
};

});

home.html

<div>
<h1>Hello {{noun.person}}</h1>
</div>

我的目录

enter image description here

开发工具网络

enter image description here

最佳答案

模板的路径是相对于主页的。您正在页面文件所在的根目录中查找模板,但它们位于子目录中:

尝试:

 $routeProvider
.when("/home", {
templateURL: "home/home.html",
controller: "HomeController"
})
.when("/about", {
templateURL: "about/about.html",
controller: "AboutController"
})

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

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