Angular Object Sharing

1

Does anyone know of a way to send an object from one angular directive to another?

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

I'm not able to go through a parameter initially since the function is called through an event.

    
asked by anonymous 30.01.2015 / 14:29

2 answers

1

Remember that services and factories are singletons. Sharing objects may not be good because, in case of multiple uses, objects will be overwritten with each call, which can compromise what you want to show on the screen. Example: directive 1 arrow object 1, directive 2 overwrites and arrows object 2. When directive 1 is to get the data, it will receive object 2 and not object 1, which would be expected.

If you are directives at different parent-child levels, you can rely on this example to access the scope of the parent directive through a child: link

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);

In the directive that will receive (scope within the link function. If using the controller use $ scope same):

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

    
31.01.2015 / 22:39
1
___ erkimt ___ Angular Object Sharing ______ qstntxt ___

Does anyone know of a way to send an object from one angular directive to another?

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

I'm not able to go through a parameter initially since the function is called through an event.

    
______ azszpr48964 ___

Remember that services and factories are singletons. Sharing objects may not be good because, in case of multiple uses, objects will be overwritten with each call, which can compromise what you want to show on the screen. Example: directive 1 arrow object 1, directive 2 overwrites and arrows object 2. When directive 1 is to get the data, it will receive object 2 and not object 1, which would be expected.

If you are directives at different parent-child levels, you can rely on this example to access the scope of the parent directive through a child: link

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

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

%code%

In the directive that will receive (scope within the link function. If using the controller use $ scope same):

%code%

    
______ ___ azszpr48961

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 {
     ...
  };
});

So do not pollute any scope or $ requires that the directives are nested.

EDIT:

The intention is that this service is only shared between the relevant directives. If in another context need to share other data (different) create another service. Never refill a service to a different task from which it was conceived.

    
___
31.01.2015 / 21:35