search

Home  >  Q&A  >  body text

angular.js - angularjs ui-router browser back

<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>

Click the a tag, ui-view displays "Hello", click the browser to go back and "Hello" still exists on the page, why is this?

ringa_leeringa_lee2721 days ago973

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-07 09:25:52

    It is recommended to read this article first about using angular-ui-router. By default, an initial state needs to be set. See the following usage examples for details.

    <!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>

    reply
    0
  • Cancelreply