Angularjs Object Sharing

Asked

Viewed 1,003 times

1

Someone knows a way to send an object from one angular directive to another?

I have 2 directives and I need access to the object in both.

I’m not getting through parameter initially since the function is called through an event.

  • You can use the $rootScope.

  • you have some example of how this could be implemented?

2 answers

2


Remember that services and Factories are singletons. Sharing objects may not be good because in the case of multiple uses, objects will be overwritten at each call, which can compromise what you want to show on the screen. Example: Directive 1 arrow object 1, Directive 2 overrides and arrow object 2. When Directive 1 picks up the data, it will receive object 2 and not object 1, which would be expected.

If you are Parents at different hierarchy levels (Parent - Child), you can use this example to access the Scope of Directive parent through a child: https://stackoverflow.com/a/18509399/1481408

If this is not your case, use the Angularjs event system.

Example: to send an event with the object in any Scope:

var objeto = { beizer : 'huehuehue' }; $rootScope.$broadcast('pega.o.objeto', objeto);

on the receiving Directive (Scope inside the link function. If you use the controller use $Scope even):

scope.$on('pega.o.objeto', function ($event, objeto) { ... tá aqui seu objeto = { beizer : 'huehuehue' } })

1

Data sharing can be through service.

angular.module('app').service('DadosCompartilhados', function() {
  this.dados = {};
});

angular.module('app').directive('diretivaA', function(DadosCompartilhados) {
  // o serviço DadosCompartilhados será compartilhado entre todas as diretivas
  return {
     ...
  };
});

This way no $Cope is polluted nor does it require the directives to be nested.

EDIT:

the intention is that this service is shared only among the directives concerned. If in another context you need to share other (different) data, create another service. Never reuse a service for a task other than the one it was designed for.

  • services are singletons. Sharing in this way objects in Directives that can reference different objects will hum everything

  • @Deividicavarzan did not understand what you refer to by "referencing different objects". what scenario can "zuar all" ?

  • Imagine someone calling the service and entering the Objetoa. Another place arrow the Objetob. This time, if someone who set the Objetoa and expects the return to be of the Objetoa type, they will receive the Objetob. And this can compromise the data that will be obtained.

  • @Deividicavarzan simple, Objetoa is kept in Serviçoa and Objetob is kept in Serviçob. I don’t mean creating a single service to maintain all application data. Alias an object like this would already exist at the angular.

  • Yes, it is, of course. But what I had understood is that this would be a service for multiple objects, in this case, with a service for each object, it works OK even.

  • One thing that works cool is to have a unique service, but in set / get vc passes the key of the object you want. So it’s a nice way to manage in a single service what you want, like access to the localStorage or something like that

  • @Deividicavarzan in counter-start loses the ability to manage the data, such as validation, transfer, etc. I believe in creating appropriate services, the system is structurally better described when giving specific purposes to each object.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.