3

This simple code has two links, "State 1" and "State 2".

When I click "State 1", it prints out "This is the first state". When I click "State 2", it prints out "This is the second state".

But then when I click my browser's back button, it still says "This is the second state". Shouldn't it change to "This is the first state"?

CodePen: http://codepen.io/anon/pen/bCohi

The code:

<html>
<head>
  <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js'></script>  
  <script type="text/javascript" src='http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js'></script>
</head>
<body ng-app="myApp">
  <a ui-sref="state1">State 1</a>
  <a ui-sref="state2">State 2</a>
  <br>
  <ui-view></ui-view>
  <script>
  var myApp = angular.module("myApp", ['ui.router']);

  myApp.config(function($stateProvider, $urlRouterProvider) {
    var stateProvider = $stateProvider;
    stateProvider.state('state1', {
      url: 'state-1',
      template: 'This is the first state'
    });
    stateProvider.state('state2', {
      url: 'state-2',
      template: 'This is the second state'
    });
  });
  </script>
</body>
</html>
1
  • Change the url (hash) on click and add a watch function to update the view on location update. Commented Jul 17, 2014 at 17:06

1 Answer 1

7

Oh - I figured it out.

url: 'state-1',
. . . . . . . . . .
url: 'state-2',

should be

url: '/state-1',
. . . . . . . . . .
url: '/state-2',
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.