gpt4 book ai didi

javascript - 显示 AngularJS 模板

转载 作者:行者123 更新时间:2023-12-03 09:25:48 26 4
gpt4 key购买 nike

<html>
<head ng-app="app">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<title>NG ROUTING</title>
<meta charset="utf-8">
</head>
<body ng-controller="homeController">
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">My Website</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
</ul>
</div>
</nav>
</header>
<div id="main">
<div ng-view></div>
</div>



<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular-route.js"></script>
<script src="js/app.js"></script>
</body>

这是我的index.html 文件,我想要做的是每次单击导航中的链接时,我希望我的模板之一显示在 ngView 中。这是我的 app.js 文件:

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

app.config(function($routeProvider){
$routeProvider.when('/', {
templateUrl: '/pages/home.html',
controller: 'homeController'
}).when('/pages/about',{
templateUrl: 'pages/about.html',
controller: 'aboutController'
}).when('/pages/contact/', {
templateUrl: '/pages/contact.html',
controller: 'contactController'
}).otherwise({
template: '/pages/routeNotFound.html',
controller: 'notFoundController'
});
});

app.controller('homeController', function($scope){
$scope.message = "Welcome to my homepage";
});

app.controller('aboutController', function($scope){
$scope.message = "Come and find out more about me and what I like to do.";
});

app.controller('contactController', function($scope){
$scope.message = "Contact me now!";
});

app.controller('notFoundController', function($scope, $location){
$scope.message = "There seems to be a problem with finding the page you wanted";
$scope.attemptedPath = $location.path();
});

我猜测我匹配 url 路径和路由(或者可能是 html 元素中的 href 链接)的方式有问题,因为控制台中没有显示错误,但没有模板出现。

模板存储在“pages”文件夹中,包含 angularJS 代码的文件存储在“js”文件夹中。

任何帮助将不胜感激。

最佳答案

<li><a href="#/"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#/about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#/contact"><i class="fa fa-comment"></i> Contact</a></li>

还有

$routeProvider
.when('/', {
templateUrl: '/pages/home.html',
controller: 'homeController'
}).when('/about',{
templateUrl: 'pages/about.html',
controller: 'aboutController'
}).when('/contact', {
templateUrl: '/pages/contact.html',
controller: 'contactController'
}).otherwise({
template: '/pages/routeNotFound.html',
controller: 'notFoundController'
});});

或者

<li><a href="#/"><i class="fa fa-home"></i> Home</a></li>
<li><a href="#/pages/about"><i class="fa fa-shield"></i> About</a></li>
<li><a href="#/pages/contact"><i class="fa fa-comment"></i> Contact</a></li>

还有

$routeProvider
.when('/', {
templateUrl: '/pages/home.html',
controller: 'homeController'
}).when('/pages/about',{
templateUrl: 'pages/about.html',
controller: 'aboutController'
}).when('/pages/contact', {
templateUrl: '/pages/contact.html',
controller: 'contactController'
}).otherwise({
template: '/pages/routeNotFound.html',
controller: 'notFoundController'
});});

关于javascript - 显示 AngularJS 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677766/

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