title: using ng-include for a template that has directives that use data retrieved by XHR
body: <p>I am using ng-include in order to include a persistent menu, that exists in all of the views of my SPA.</p>
  
  <p>The problem is that I want to display different options and content in this menu per each user type(admin, guest, user etc.), and this requires the service function authService.loadCurrentUser to be resolved first.</p>
  
  <p>For the purpose of managing this content easily and comfortably, I have created a simple directive, that takes an attribute with the required access level, and at the compile phase
  of the element, if the permissions of the given user are not sufficient, removes the element and it's children.</p>
  
  <p>So after failing miserably at trying to make the ng-include go through the routeProvider function, I've tried to use ng-init, but nothing seems to work, the user role remain undefined at the time that I am logging it out.</p>
  
  <p>I am thinking about trying a new approach, and making the entire menu a directive that includes the template that is suitable for each user type, but first I would like to try and solve this matter.</p>
  
  <p>Directive:</p>
  
  <pre><code>'use strict';
  /* Directives */
  angular.module('myApp.directives', []).
  directive('restrict', function(authService){
      return{
          restrict: 'A',
          prioriry: 100000,
          scope: {
              // : '@'
          },
          link: function(){
              // alert('ergo sum!');
          },
          compile:  function(element, attr, linker){
              var user = authService.getUser();
                  if(user.role != attr.access){
                  console.log(attr.access);
                  console.log(user.role);//Always returns undefined!
                      element.children().remove();
                      element.remove();           
                  }
  
          }
      }
  });
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>'use strict';
  /* Services */
  angular.module('myApp.services', []).
  factory('authService', function ($http, $q) {
      var authServ = {};
      var that = this;
      that.currentUser = {};
      authServ.authUser = function () {
          return $http.head('/users/me', {
              withCredentials: true
          });
      },
      authServ.getUser = function () {
          return that.currentUser;
      },
      authServ.setCompany = function (companyId) {
          that.currentUser.company = companyId;
      },
      authServ.loadCurrentUser = function () {
          var defer = $q.defer();
          $http.get('/users/me', {
              withCredentials: true
          }).
          success(function (data, status, headers, config) {
              console.log(data);
              that.currentUser.company = {};
              that.currentUser.company.id = that.currentUser.company.id ? that.currentUser.company.id : data.main_company;            
              that.currentUser.companies = [];
              for (var i in data.roles) {
                  that.currentUser.companies[data.roles[i]['company']] = data.roles[i]['company_name'];
                  if (data.roles[i]['company'] == that.currentUser.company.id){
                      that.currentUser.role = data.roles[i]['role_type']; 
                      that.currentUser.company.name = data.roles[i]['company_name'];
                      // console.log(that.currentUser.role);
                  } 
              }
              // defer.resolve(data);
              defer.resolve();
          }).
          error(function (data, status, headers, config) {
              that.currentUser.role = 'guest';
              that.currentUser.company = 1;
              defer.reject("reject");
          });  
      return defer.promise;
      }
  
      return authServ;
  });
  </code></pre>
  
  <p>Menu controller:</p>
  
  <pre><code>angular.module('myApp.controllers', []).
  controller('menuCtrl', function($scope, $route, $location, authService){
      //TODO: Check if this assignment should be local to each $scope func in order to be compliant with 2-way data binding
      $scope.user = authService.getUser();
      console.log($scope.user);
      // $scope.companies = $scope.user.companies;
      $scope.companyOpts = function(){
          // var user = authService.getUser();
          if(typeof $scope.user.company == 'undefined')
              return;
  
              var companies = [];
              companies[$scope.user.company.id] = $scope.user.company.name;
              for(var i in $scope.user.companies){
                  if(i != $scope.user.company.id){
                      companies[i] = $scope.user.companies[i];    
                  }
              }
              // console.log(companies);
              // if(nonCurrentComapnies.length &gt; 0){
                  console.log(companies);
                  return companies;
              // }
      }
  
  
  
      $scope.$watch('user.company.name', function(company){
          for(var i in $scope.user.companies)
              if(company == $scope.user.companies[i].id)
                  authService.setCompany(i);
      });
      $scope.$watch(function(){return authService.getUser().company; }, function(company){
          //Refresh the page on company change here, first time, and each time the user changes the select
          // $scope.companyOpts();
          // $scope.currentComapany = company;
      })
  ;})
  </code></pre>
  
  <p>Main SPA HTML page:</p>
  
  <pre><code>&lt;div ng-init="authservice.loadCurrentUser" ng-include src="'partials/menu.html'"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>menu element that should be visible only to the admin:</p>
  
  <pre><code>&lt;ul class="left" restrict access="admin"&gt;
    &lt;li&gt;You are the admin!&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Thanks in advance for any assistance!</p>