title: How to Access directive data from a controller from a seperate file
body: <p>Access directive data from a controller from a separate file. Wanted to access the data from <code>SheetJSImportDirective</code> which is the <code>$scope.$apply</code> function that contains  <code>$scope.opts.data</code>. The  <code>$scope.opts.data</code> is the data I wanted to access the controller on a separate file. Don't worry about the integrations of the directive and controllers including factories I have already made it possible, just wanted to know the best way to access the data from the directive to a controller on a separate file. I wanted to know the supported way or the best way of making it possible. Thank you.</p>
  <h1>And in my Controller with a function like for example below.</h1>
  <pre><code>me.accessDataHere = function() {
    //When this function is called , access  $scope.opts.data = data; from SheetJSImportDirective
  }
  </code></pre>
  <h1>Directive Function</h1>
  <pre><code>function SheetJSImportDirective() {
    return {
      scope: { opts: '=' },
      link: function($scope, $elm, reportsDirCtrl) {
        $elm.on('change', function(changeEvent) {
          var reader = new FileReader();
  
          reader.onload = function(e) {
            var bstr = e.target.result;
            var wb = XLSX.read(bstr, { type: 'binary' });
            var wsname = wb.SheetNames[0];
            var ws = wb.Sheets[wsname];
  
            var aoa = XLSX.utils.sheet_to_json(ws, { header: 1, raw: false });
            var cols = [];
            for (var i = 0; i &lt; aoa[0].length; ++i) cols[i] = { field: aoa[0][i] };
  
            var data = [];
            for (var r = 1; r &lt; aoa.length; ++r) {
              data[r - 1] = {};
              for (i = 0; i &lt; aoa[r].length; ++i) {
                if (aoa[r][i] == null) continue;
                data[r - 1][aoa[0][i]] = aoa[r][i]
              }
            }
  
            /* update scope */
            $scope.$apply(function() {
              $scope.opts.columnDefs = cols;
              $scope.opts.data = data;
              testdata()
              console.log(&quot;JSON Data :&quot;, $scope.opts.data)
  
            });
          };
  
          reader.readAsBinaryString(changeEvent.target.files[0]);
        });
      }
    };
  }
  </code></pre>