title: angularJS: Error: $scope is not defined
body: <p>I want to run a service that read total unread message when user visits a few particular page. I'm using <code>resolve</code> for this. I set up a factory which communicates with the backend through a http call and the backend returns the count of total messages and I wanna show this in html page but all I am getting is error.</p>
  
  <pre><code>( function () {
  
      var countAllUnreads = function($location, $q, AuthService3) 
      {
          var deferred = $q.defer();
          AuthService3.fetchUnreadNotifications().then(function (res)
        {
          console.log('this is me');
          $scope.numOfNotifications =res.data.totalUnread;
        });
      }
      angular.module('myApp', [
          'ngRoute',
          'myApp.login',
          'myApp.home',
          'myApp.directory',
          'myApp.forgotpassword',
          'myApp.myProfile',
      ])
  
      .factory('AuthService3', ["$http", "$location", function($http, $location){
         var baseUrl = 'api/';
        var fetchUnreadNotifications = function()
         {
  
          return  $http.post(baseUrl + 'getAllUnreadNotifications');        
        }    
  
          return {fetchUnreadNotifications: fetchUnreadNotifications} ; 
      }])
      .config(['$routeProvider', function($routeProvider) {
          $routeProvider
          .when('/login', {
              controller: 'LoginController',
              templateUrl: 'app/components/login/loginView.html',     
              controllerAs: 'vm'
          })
          .when('/forgotpassword', {
              controller: 'ForgotpasswordController',
              templateUrl: 'app/components/forgotpassword/forgotpasswordView.html',
              controllerAs: 'vm'
          })
          .when('/directory/:directoryType', {
              controller: 'DirectoryController',
              templateUrl: 'app/components/directory/directoryView.html',
              resolve: {notifications:countAllUnreadsn,},     
              controllerAs: 'vm'
          })
          .when('/home', {
              controller: 'HomeController',
              templateUrl: 'app/components/home/homeView.html',
              resolve: {notifications:countAllUnreadsn,},         
              controllerAs: 'vm'          
          })
          .when('/myprofile', {
              controller: 'MyProfileController',
              templateUrl: 'app/components/profile/myProfileView.html',
              resolve: {notifications:countAllUnreadsn,},     
              controllerAs: 'vm'
          })
          .otherwise({
              redirectTo: '/login'
          });
      }]);
  
  })();
  </code></pre>