繁体   English   中英

我如何在AngularJs中处理hashchange事件

[英]How can I act on a hashchange event in AngularJs

我是AngularJs的新手。 我想做的是在hashchange事件上更新页面。

我目前正在做的事情(并且知道这不是“正确的”方法):

<!DOCTYPE html>
<html>
<head>
<style>
    #hashdrop {
      display:block;
      border: 1px solid gray;
      width: 500px;
      overflow: auto;
      background-color: rgb(241,241,241);
      border-radius: 4px;
      text-align: center;
      vertical-align: middle;
      line-height: 100px;
      font-size: large;
      height: 100px;
    }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
    var example = function($scope) {
      $scope.lastHash = location.hash.slice(1);
      $(window).on('hashchange', function (event) {
        $scope.lashHash = location.hash.slice(1);
        $scope.$apply();
      });
    };
</script>
<meta charset=utf-8 />
</head>
<body ng-app ng-controller="example">
  <div id="hashdrop" >
    {{lastHash}}
  </div>
</body>
</html>

(可以将其复制粘贴到空白html文件中并运行。对我来说,jsbin未能正确检测到haschange。)

这实际上是行不通的。 由于某种原因,该值未更新,因此我认为在angularjs中有一种“正确”的方法。
我将了解如何正确执行此操作,更具体地说,如何在此示例中更正代码以使其“成角度地工作”。

谢谢!

更新1好的,在阅读了有关$ location的注释后,我找到了一些信息并将我的应用程序更改为:

<!DOCTYPE html>
<html>
<head>
<style>
    #hashdrop {
      display:block;
      border: 1px solid gray;
      width: 500px;
      overflow: auto;
      background-color: rgb(241,241,241);
      border-radius: 4px;
      text-align: center;
      vertical-align: middle;
      line-height: 100px;
      font-size: large;
      height: 100px;
    }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
    angular.module('example',[]).config(function($locationProvider, $routeProvider) {
      $locationProvider.html5Mode(true);
    });
    var example = function($scope, $location) {


    };
</script>
<meta charset=utf-8 />
</head>
<body ng-app="example" ng-controller="example">
  <div id="hashdrop" >
    Hash = {{$location.hash()}}
  </div>
</body>
</html>

仍然不起作用。 什么都没有显示在输出中。

AngularJs在所有正常路由中使用url中的哈希,这是浏览页面的默认方式。 您可以像在后端MVC框架(如Django)中那样使用routeProvider来定义url方案,并且routeProvider会侦听哈希中的更改并相应地加载新内容。

我建议做本教程。 这是相当不错的(即使它错过了很多东西)。 步骤7中将演示路线。

只是警告。 Angular假定您使用哈希作为处理URL和路由的主要方式,因此,在更改URL的哈希部分时,routeParams不会仅更改页面的一部分。 为此,您可以切换到使用get参数,也可以使用更灵活的ui-router之类的东西。

您可以使用angular.element而不是jQuery绑定到hashchange事件,从而可能减少依赖关系。

将此包含在您的控制器中。

angular.element(window).bind('hashchange', function() {
  console.log('hash has changed');
});

哈希更改时,此代码将记录“哈希已更改”。

不确定是否可以解决您的问题(正如Kevin Beal所指出的那样,您的代码中还有一些更时髦的东西),但是您不清楚您在说什么“不起作用”。

值得一提的是angular.element 使用jQuery(如果可用) ,因此,无论如何要使用jQuery, angular.element(window)$(window)不会做任何不同的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM