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):
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..
Does anyone have ideas about this? Thanks!
