Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:18:14+00:00 2026-06-14T17:18:14+00:00

I have some data called foo which lives in a scope which is parent

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:18:15+00:00Added an answer on June 14, 2026 at 5:18 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data which (quite reasonably) uses null and false for different meanings.
I have some data which i need to make some statistics. I need to
I have a helper method which encrypts some data on the iPhone. If the
I have three classes organized in following manner. Foo is a template class, Bar
I have some data loaded as a np.ndarray and need to convert it to
I have some data from log files and would like to group entries by
I have some data files in the resources/ folder for my Rubymotion project. How
I have some data and need to create a json file with this structure
I have some data that is stored in a TIMESTAMP(6) WITH TIMEZONE column in
I have some data with one row of headers and one or more rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.