0

I want to be able to use the variable "videoUrlId" from the controller 'useSpreadsheetData' below in my directive 'meetings'. How can I do this? I have looked at require but could not get it to work.

Controller:

app.controller('useSpreadsheetData', ['$scope', '$sce', 'getSpreadsheetData',      
function($scope, $sce, getSpreadsheetData){
for(var x in videos) {
      if(videos[x].switchValue) {
        var videoUrlId = videos[x].videoUrl;
        $scope.videoUrlId = videoUrlId;
        break;
      }
    }
};

Directive:

app.directive('meetings', [ 'getCalendar', '$timeout', '$window',  
function(getCalendar, $timeout, $window){
return {
    restrict: 'E',
    templateUrl: 'scripts/directives/meetings.html',
    controller: 'useSpreadsheetData',
    link: function(scope){
    //Use videoUrlId variable here
    }
}
}]);
5
  • 1
    You can access videoUrlId in your link function with scope.videoUrlId Commented Nov 23, 2015 at 22:29
  • In order to share data between directives and controllers or controllers and controllers, you should use Services. Take a look at this post and this other one post. Commented Nov 23, 2015 at 22:32
  • @rob i get undefined when I access that variable in my link function. Commented Nov 23, 2015 at 22:53
  • is the code in your controller actually running? From what you've posted videos will always be undefined Commented Nov 23, 2015 at 22:57
  • @AndreaM16 My controller is already getting data from a service. Is there a way to get a variable from a controller and use it in my directive link function without making another service? Commented Nov 23, 2015 at 22:57

1 Answer 1

1

Since you mentioned you attempted to use require, I have to assume the meetings directive will be a child element somewhere within the useSpreadsheetData controller, however without seeing your HTML, we can't be sure.

As you're not utilizing an isolate scope, your directive will prototypically inherit from the parent controller above itself, in this case useSpreadsheetData. As a result, we can simply get videoUrlId by accessing it via an interpolated expression: {{videoUrlId}}. Note the template in the meetings directive. It'll also be available within link or controller via scope.videoUrlId and $scope.videoUrlId, respectively.

Plunker: http://plnkr.co/edit/MZIgXEiku4Z2PLzl3apz

HTML

<div ng-controller="useSpreadsheetData">
  Controller: <code>videoUrlId = {{videoUrlId}}</code><br>
  Directive: <meetings></meetings>
</div>

JavaScript

app.controller('useSpreadsheetData', function($scope) {

  var videos = [service call];

  for (var x in videos) {
    if (videos[x].switchValue) {
      var videoUrlId = videos[x].videoUrl;
      $scope.videoUrlId = videoUrlId;
      break;
    }
  }
});

app.directive('meetings', ['$timeout', '$window',
  function($timeout, $window) {
    return {
      restrict: 'E',
      template: '<code>videoUrlId = {{videoUrlId}}</code>'
    }
  }
]);

Output

Controller: videoUrlId = /1
Directive: videoUrlId = /1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.