Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you access scope variables of a number of directives?
    primarykey
    data
    text
    <p>I have a directive representing a person with location attributes that change. I'll like to access all the locations together and plot them on a map using something like the angular-leaflet-directive. How can I access these variables in one place? I think I'm really close to getting it working but I don't know which scope has access to all the directives variables. Here is what I have so far?</p> <p>Index.html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html ng-app="app"&gt; &lt;head&gt; &lt;meta charset=utf-8 /&gt; &lt;title&gt;&lt;/title&gt; &lt;script src="http://code.jquery.com/jquery-1.10.1.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.angularjs.org/1.2.0-rc.3/angular.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.angularjs.org/1.2.0-rc.3/angular-resource.min.js"&gt;&lt;/script&gt; &lt;script src="http://code.angularjs.org/1.2.0-rc.3/angular-animate.min.js"&gt;&lt;/script&gt; &lt;script src="app.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body ng-controller='MainCtrl'&gt; &lt;a href='' class='btn' ng-click='addPerson()'&gt;Add new person&lt;/a&gt;&lt;Hr&gt; &lt;div id='people'&gt; &lt;person lat="0.0" lng="0.0"&gt;&lt;/person&gt; &lt;person lat="0.0" lng="0.0"&gt;&lt;/person&gt; &lt;person lat="0.0" lng="0.0"&gt;&lt;/person&gt; &lt;/div&gt; &lt;hr&gt; &lt;div class="map"&gt; &lt;!-- this will be a directive representing a map --&gt; How do I access the lat and lon of each directive here? So I can plot them all on a map (which is also a directive ...) &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>App.js</p> <pre><code>var app = angular.module('app', []); app.directive('person', function ($compile, $timeout) { function link ($scope, elem, attrs, ctrl) { $scope.lat = attrs.lat; $scope.lng = attrs.lng; $timeout( function changePosition() { console.log('Changing position ...'); $scope.lat = Math.random() $scope.lng = Math.random() $timeout(changePosition, 2000); }, 2000); } return { restrict: 'E', replace: true, template: "&lt;span&gt;Current Lat={{lat | number:2}}, Lng={{lng | number:2}}&lt;br&gt;&lt;br&gt;&lt;/span&gt;", link : link, scope: {}, } }); app.controller('MainCtrl', function ($scope, $compile) { $scope.addPerson = function() { console.log('Adding new person'); var lat = Math.random() var lng = Math.random() angular.element('#people').append($compile('&lt;person lat="'+lat+'" lng="'+lng+'"&gt;&lt;/person&gt;')($scope)); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload