javascript - How to access another views $scope in angular? -


good morning,

i use angularjs build web application. make use of embed plugin of angularjs youtube. precise: https://github.com/brandly/angular-youtube-embed

now pause video following event listener: player.pausevideo();. have added 1 button.

the youtube embed on same page above function called. markup of object is:

<youtube-video video-id="video" id="youtube-video" player="player" style="height:450px; width: 100%"></youtube-video> 

the player attribute on object 1 should use link between object , function call.

but want access player object on separate view html document. function on page doesn't pause video on view.html

does know how access object on page using similar call player.pausevideo();.

if there questions, ask me! thank in advance.

i'm assuming "player object" refering scope variable $scope.player , "another page" mean view, different scope, rendered same html document view player resides. if right these assumptions, have 2 options, both valid w.r.t. "angular way".

using global controller

you introduce page-wide controller (lets call mastercontroller) in html document , define $scope.player there. can access $scope.player in every controller nested mastercontroller. note must not assign value $scope.player on nested controllers, block prototypical inheritance mechanism.

using events

every scope in angular child $rootscope. using $rootscope.$broadcast in controller need trigger function player , $scope.$listen in controller player attached to, can control player every possible place in app.


for deciding of ways better use-case, consider following properties:

  1. events 'one-way roads', if need retrieve value player (say, it's current play position) outside of players scope, need implement same mechanism other way around, whereas using global controller easy writing $scope.player
  2. events support development of uncoupled, reuseable components. if implement video hub in addition youtube supports vimeo , other platforms abstract away control of different plugins using events , drop-in new video platforms without need modify other views controllers.

i recommend book "learning javascript design patterns" addy osmani , angular guide on scopes


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -