Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
    
Change the url (hash) on click and add a watch function to update the view on location update. –  ackerman Jul 17 at 17:06

1 Answer 1

up vote 1 down vote accepted

Oh - I figured it out.

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

should be

url: '/state-1',
. . . . . . . . . .
url: '/state-2',
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.