简体   繁体   English

单击浏览器的“返回”按钮时,AngularJS的$ location.path()不会更改吗?

[英]AngularJS's `$location.path()` is not changed when I click BACK button of browser?

I was using videoJS to load video dynamically, and want to destroy the video player when route or location changes, I tried to wrap it in a directive like this (Coffeescript code): 我当时使用videoJS动态加载视频,并且想在routelocation发生变化时销毁视频播放器,所以我尝试将其包装在这样的指令中(咖啡代码):

angular.module('myModule')
.directive('myDirective', ($location)->
  return {
  restrict: 'A'
  scope: {rawUrl: '='}
  link: (scope, element, attrs) ->    
    attrs.type = 'application/x-mpegURL'
    setup =
      controls: true
      preload: 'auto'
      autoplay: true
      height: window.innerHeight
      width: window.innerWidth
      'data-setup': '{example_option: true}'

    myPlayer = element

    scope.$watch('location.path()', ->
      # TODO: destroy videoJS when user leaves video page
      console.log($location.path())
      if not $location.path().match '/video/'
        console.log("destroy you!")
        videojs(attrs.id).dispose()

    )
    scope.$watch('rawUrl.videoUrl', (newvalue)->
      if newvalue
        player = videojs(attrs.id, setup, ->
          console.log('created')
          this.src(newvalue)
          return
        )
        videojs(attrs.id).ready( ->
          console.log('ready!')
          myPlayer = this
          aspectRatio = 9 / 16;
          resizeVideoJS = ->
            myPlayer.width(window.innerWidth).height(window.innerHeight);            )


   )
  }
)

However, I found the callback function within scope.$watch('location.path(), mycallback) is not called if I changed the page using broswer's BACK button.. 但是,我发现scope.$watch('location.path(), mycallback)的回调函数scope.$watch('location.path(), mycallback)如果我使用浏览器的BACK按钮更改页面scope.$watch('location.path(), mycallback)则不会调用scope.$watch('location.path(), mycallback)

Does anyone have ideas about this? 有人对此有想法吗? Thanks! 谢谢!

在此处输入图片说明

只需使用scope.$on('$destroy', destroyCallback)

I'm not 100% sure this will work, but you could try to wrap the $location.path() call inside of a function defined on the scope eg 我不是100%肯定会行得通,但是您可以尝试将$ location.path()调用包装在范围内定义的函数中,例如

scope.location = { path : function () { return $location.path(); } }

Again, I'm not sure if the route change will occur prior to the digest cycle. 同样,我不确定路由更改是否会在摘要周期之前发生。

If it does, you could, instead, listen for the location-change event ( Angular doc on $location events ) using: 如果是这样,您可以改为使用以下方法监听位置更改事件( 有关$ location事件的Angular文档 ):

scope.$on('$locationChangeStart', function (event) { /* code you had in your watcher should still work fine here */ });

Edit: Editing $route events to $location events (refer to new link in documentation and this answer . $location events should be broadcasted upon changes in the browser's url. 编辑:将 $ route事件编辑为$ location事件(请参阅文档中的新链接, 此答案 。$ location事件应在浏览器的URL更改时广播。

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

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