javascript - Accessing objects between components/controllers in Angularjs -


i need share objects between controllers in angularjs , have made works, feels bit clumsy me. i'm wondering if have acceptable, or if there's preferred way so.

in componenta have object want access componentb.

app.component("componenta", {     controller: function ($scope, $rootscope, $compile) {          //the object contents want         $scope.objecttoaccess = {name: 'object', value: 3};          //receives broadcast componentb, returns object         $rootscope.$on("getobject", function () {             $rootscope.$broadcast('receiveobject', $scope.objecttoaccess);         });     } }  app.component("componentb", {     controller: function ($scope, $rootscope, $compile) {          $scope.object = {name : null, value: null};          //receives broadcast of object , sets value         $rootscope.$on("receiveobject", function (event,object) {             console.log(object);             $scope.object = object;         });          //broadcast object contents         $rootscope.$broadcast('getobject');        } } 

this works, feels complicated lot of , forth communication. there built angular designed handle kind of thing, or have considered acceptable?

in opinion $scope events should used in cases data changes subscribed , not requested.

instead can use service hold data , refer in controllers. since services singleton both controllers share same instance , hence can share data.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -