问题描述
<body>
<p ui-view></p> <a ui-sref='test'>点击</a>
</body>
<script type='text/javascript'>var app = angular.module(’myApp’, [’ui.router’]); app.config(function($stateProvider, $urlRouterProvider){
$stateProvider .state('test', { url: ’/test’, template:’<p>你好</p>’,});
}); </script>
点击a标签,ui-view显示“你好”,点击浏览器后退“你好”还存在页面上,这是为什么?
问题解答
回答1:建议先看一下,这篇文章 angular-ui-router使用。默认的话都需要设置一个初始状态的哈,具体看一下下面的使用示例。
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>UI-Router</title> <script src='https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js'></script> <script src='https://cdn.bootcss.com/angular-ui-router/1.0.3/angular-ui-router.min.js'></script></head><body ng-app='myApp'><p ui-view></p><a ui-sref='login'>登录页</a><a ui-sref='home'>首页</a><script type='text/javascript'> var app = angular.module(’myApp’, [’ui.router’]); app.config(function ($stateProvider, $urlRouterProvider) {$stateProvider.state('home', { url: ’/home’, template: ’<p>首页</p>’,}).state('login', { url: ’/login’, template: ’<p>登录页</p>’,});$urlRouterProvider.otherwise(’/login’); });</script></body></html>