angularjs - pass data between controllers -


i'm stating learn angularjs, coming lot of different mv* frameworks. framework, i'm having trouble passing data between controllers.

suppose have screen input (input.html) , controller, let's inputctrl.
there's button on view takes screen, let's approve (approve.html) controller approvectrl.
approvectrl needs data inputctrl. seems common scenario in bigger applications.

in previous mv* frameworks, handled (pseudo-code):

   var self = this;    onclick = function() {           var approvectrl = di.resolve(approvectrl);           approvectrl.property1 = self.property1;           approvectrl.property1 = self.property2;           self.router.show(approvectrl);      }             
  • it work controller- first. create controller first, having chance put in right state; afterwards view gets created.

now, in angularjs, i'm handling like:

 var self = this;  onclick = function(){           self.$locationservice.path('approve');         } 
  • this works view-first. view / route navigate, controller gets created framework.

i find hard control state of created controller , pass data it. i've seen , tried following approaches, have it's own issues in opinion:

  1. inject shared service inputctrl & approvectrl , put data shared on service
    • this looks dirty work-around; state in shared service becomes global state, while need pass data approvectrl
    • the lifetime of shared service way longer need - pass data approvectrl
  2. pass data in $routeparams
    • this gets quite messy when having pass lot of parameters
  3. use $scope events
    • conceptually, not use events - need pass data approvectrl, nothing event-ish
    • this quite cumbersome; have send event parent first, broadcast it's children

am missing here? creating many small controllers? trying hold on habits other frameworks here?

in terms of structure angularjs more modular mvc one.

classic mvc describes 3 simple layers interact each other in such way controller stitches model view (and model shouldn't rather work view directly or vice versa).

in angular can have multiple, optional, entities can interact between each other in multiple ways, example:

possible interactions

that's why there multiple ways of communicating data between different entities. can:

or

  • send messages using ajax backend
  • send messages using external system (such mq)

...and lot more. due diversity angular allows developer/designer choose way comfortable , carry on. recommend reading angularjs developer guide can find blessed solutions common problems.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -