在AngularJS中使用ui-router加载两个模板的方法如下:
<script src="angular.js"></script>
<script src="angular-ui-router.js"></script>
var app = angular.module('myApp', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home.html'
})
.state('about', {
url: '/about',
templateUrl: 'about.html'
});
});
在上述代码中,我们定义了两个状态(state):'home'和'about',并指定了它们对应的URL和模板文件。
<!-- home.html -->
<div>
<h1>Welcome to Home Page</h1>
<!-- Home page content goes here -->
</div>
<!-- about.html -->
<div>
<h1>About Us</h1>
<!-- About page content goes here -->
</div>
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>UI-Router Example</title>
</head>
<body>
<header>
<!-- Header content goes here -->
</header>
<div ui-view></div>
<footer>
<!-- Footer content goes here -->
</footer>
</body>
</html>
在上述代码中,我们使用了ui-view指令来显示路由对应的模板。当访问不同的URL时,ui-router会自动加载对应的模板,并将其插入到ui-view指令所在的位置。
这样,当访问应用的根URL时,会加载'home.html'模板;当访问'/about'URL时,会加载'about.html'模板。
推荐的腾讯云相关产品:腾讯云云服务器(CVM)和腾讯云对象存储(COS)。
领取专属 10元无门槛券
手把手带您无忧上云