使用requireJS和AngularJS在应用程序中后退和前进浏览器按钮

时间:2013-07-03 02:05:04

标签: angularjs requirejs angular-routing

我正在使用requireJS和AngularJS来编写我的webapp。除浏览器后退和前进历史外,它的工作原理很好。这就是我的网址的样子:

.when('/spaces', { ...

<a href="#spaces">这会将我带到页面,但后退和前进按钮不执行任何操作。如果我将其更改为<a href="#/spaces">,则后退和前进按钮会起作用,但模板不会出现。

我正在使用此样板:https://github.com/fdore/AngularJS-Boilerplate

你能看看这个并尝试解决它吗?

1 个答案:

答案 0 :(得分:1)

当我尝试时,后退按钮对我有用。检查此HTML:

<body ng-app="app">
  <div>

    <ul>
      <li><a href="#/">Home</a></li>
      <li><a href="#/about">About</a></li>
    </ul>
    <div ng-view=""></div>
  </div>
</body>

这个JavaScript:

angular.module('app',['ngResource']);
angular.module('app').config(function($routeProvider){
        $routeProvider.when('/index',{
        template: "HELLO {{name}}",
        controller: "IndexCtrl"
    }).when('/about',{
        template: "HELLO {{name}}",
        controller: "AboutCtrl"
    }).otherwise({
      template:'HELLO {{name}}',
      controller: "IndexCtrl"
    })
});    

angular.module('app').controller('IndexCtrl',function($scope){
    console.log("Loading Index");
    $scope.name = "Index";
});
angular.module('app').controller('AboutCtrl',function($scope){
    console.log("Loading About");
    $scope.name = "About";
});

当我运行此功能时,导航可以单击链接和前进/后退按钮。