0

I have a controller, which references a directive. The directive has its own controller. My Directive has ng-file-upload (which is another directive in itself).

I have some fields in the directive which needs to be passed back as an array to the main controller. How do I do this?

Cheers!

Immortal
  • 1,233
  • 4
  • 20
  • 47

2 Answers2

1

Are you perhaps using an isolate scope in your directive? You can use = to bind a scope value to the controller scope like so:

angular.module('appName').directive('directiveName', function() {

  return {
    restrict:'E',
    scope: { boundScopeValue : '=' },
    link: function(scope) {
      scope.boundScopeValue = 'Beam me up scotty!';
    }
  }

}

For more info on isolate scope and various scope methods, check out the answers on this great SO question: What is the difference between '@' and '=' in directive scope in AngularJS?

Another great option would also be to define a function in your controller, then call it in your directive when you want to pass values in and do things in the controller.

Community
  • 1
  • 1
joel.software
  • 1,505
  • 1
  • 12
  • 15
1

Isolate Scope would be the best choice for mapping outer scope members inside inner scope of directive.

However, the example for the second option is below.

<div ng-controller="MyCtrl">
    <div my-directive callback-fn="ctrlFn(arg1)"></div>
</div>


var app = angular.module('myApp',[]);

app.directive('myDirective', function() {
    return {
        scope: { someCtrlFn: '&callbackFn' },
        link: function(scope, element, attrs) {
            scope.someCtrlFn({arg1: 22});
        },
    }
});

app.controller('MyCtrl', function($scope) {
    $scope.ctrlFn = function(test) {
        console.log(test);
    }
});
Pitchai P
  • 1,317
  • 10
  • 22
  • This is a valid answer however I do not wish to pass a function reference as scope variable, that is not my requirement. Thanks for your response. +1 for the answer though :) – Immortal Dec 01 '15 at 11:24