0

I have created a service that performe the logout and saves the username of the user logged.

 angular.module('MetronicApp').service('userService', ['$http', function($http) {

    /**
     * Log out user session
     * @return {Object}         The status of the operation
     */
    this.logout = function() {
      return $http.post('/app/authenticate/logout');
    };

 }]);

Then i use this service in the controller of a directive

MetronicApp
  .directive('headerWidget', function() {
    return {
      restrict: 'E',
      scope: false,
      templateUrl: '../../widgets/header/widget.html',
      controller: ['$scope', '$filter', 'userService', function($scope, $filter, userService) {
        $scope.username = window.global.username;
        $scope.role = 'Owner';
        $scope.userThumbnailUrl = window.global.userThumbnailUrl;


        $scope.logout = function() {
          userService
            .logout()
            .success(function(data, status, headers, config) {
              // TODO: manage errors
              if (data.status === 'ok')
                window.open('/app', '_self');
            })
            .error(function(data, status, headers, config) {
              // TODO: manage errors
            });
        };
      }],
       link: function(scope, elem, attrs) {
            if (attrs.ngClick || attrs.href === '' || attrs.href === '#') {
                elem.on('click', function(e) {
                    e.preventDefault(); // prevent link click for above criteria
                });
            }
        }
    }
  });

But when the dashbord is loaded i get this error enter image descriptionhere

And the username is not displayed..

enter image description here

Any suggestion?

Fr4ncx
  • 356
  • 6
  • 22

1 Answers1

0

You need to inject the service to the directive to be able to then pass it to the controller. To do this:

MetronicApp.directive('headerWidget', ['userService', function(userService){
...
}]);
Craveiro
  • 503
  • 5
  • 15
  • Unknown provider: userServiceProvider <- userService <- headerWidgetDirective – Fr4ncx Jan 20 '16 at 13:31
  • Seems to work fine: https://plnkr.co/edit/9HLPEtekZvVjDWy5eR2P?p=info Please check. Maybe something is going wrong when importing your files? – Craveiro Jan 20 '16 at 15:03
  • but u don't use the directive into the html.. i don't know how to fix that :S – Fr4ncx Jan 20 '16 at 15:14
  • please change the plnkr so I can replicate your error. it runs so I'm assuming all the code you showed me, with the suggested changes, is correct and wouldn't produce the reported error. – Craveiro Jan 20 '16 at 15:23