Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between directives

Tags:

I have some data called foo which lives in a scope which is parent to three children:

<div ng-init="foo=[1, 2, 3]">     <bar foo="{{foo}}" baz="{{odp}}" />     <mpq foo="{{foo}}" bats="{{maktz}}" />     <ktr foo="{{foo}}" otr="{{ompg}}" /> </div>  bar.scope = {foo: '=', baz: '@'}; mpq.scope = {foo: '=', bats: '@'}; ktr.scope = {foo: '=', otr: '@'}; 

What is the best way to share foo between those three directives? Options include:

  • Use an isolated scope to pass in foo three times, thereby duplicating it across four scopes
  • Have the child directives inherit the parent scope, and find baz, bats, or otr on attrs
  • Put foo on the $rootScope and inject that into the child directives

Or is there another approach that is better?

like image 582
Nick Heiner Avatar asked Nov 20 '12 21:11

Nick Heiner


People also ask

What are directdirectives?

Directives are classes that add additional behavior to elements in your Angular applications. Use Angular's built-in directives to manage forms, lists, styles, and what users see. See the live example / download example for a working example containing the code snippets in this guide. The different types of Angular directives are as follows:

What are the benefits of sharing data with a service?

Unrelated Components: Sharing Data with a Service 1 It will always return the current value on subscription - there is no need to call onnext 2 It has a getValue () function to extract the last value as raw data. 3 It ensures that the component always receives the most recent data. More ...

What are the types of directives in HTML?

This type of directive is the most common directive type. Attribute directives —directives that change the appearance or behavior of an element, component, or another directive. Structural directives —directives that change the DOM layout by adding and removing DOM elements.

How many different methods for sharing data between angular components?

Four different methods for sharing data between Angular components. 892 words. Learning Angular? Check out the full Angular 9 Course Data sharing is an essential concept to understand before diving into your first Angular project.


1 Answers

You can create a factory that you can pass to each directive or controller. That will make sure you only have one instance of the array at any given time. EDIT: The only gotcha here is to make sure you're setting reference types and not primitive types on your directive scopes, or you'll end up duplicating the values in each scope.

Here is an example on Plnkr.co

app.controller('MainCtrl', function($scope, dataService) {   $scope.name = 'World';   //set up the items.   angular.copy([ { name: 'test'} , { name: 'foo' } ], dataService.items); });  app.directive('dir1', function(dataService){   return {     restrict: 'E',     template: '<h3>Directive 1</h3>' +      '<div ng-repeat="item in data.items">' +        '<input type="text" ng-model="item.name"/>' +      '</div>',     link: function(scope, elem, attr) {       scope.data = dataService;     }   }; });  app.directive('dir2', function(dataService){   return {     restrict: 'E',     template: '<h3>Directive 2</h3>' +      '<div ng-repeat="item in data.items">' +        '<input type="text" ng-model="item.name"/>' +      '</div>',     link: function(scope, elem, attr) {       scope.data = dataService;     }   }; });  app.directive('dir3', function(dataService){   return {     restrict: 'E',     template: '<h3>Directive 3</h3>' +      '<div ng-repeat="item in data.items">' +        '<input type="text" ng-model="item.name"/>' +      '</div>',     link: function(scope, elem, attr) {       scope.data = dataService;     }   }; });  app.factory('dataService', [function(){   return { items: [] }; }]); 

HTML

  <dir1></dir1>   <dir2></dir2>   <dir3></dir3> 
like image 85
Ben Lesh Avatar answered Oct 05 '22 02:10

Ben Lesh