Reputation: 356
So I am having a bit of trouble. I have looked through all of the previous solutions from Injecting service to Directive, but I really have no idea what I'm doing wrong. I have an authServices shown below.
app.factory('authService', ['$http', function ($http) {
var authServiceFactory = {};
var _authentication = {
isAuth: false,
userName: ""
};
var _login = function (loginData) {
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
}
appFactory.login = _login;
return appFactory;
}]);
I am injecting it via the method they had proposed.
app.directive('headerNotification', ['authService', function (authService) {
return {
templateUrl: 'app/scripts/directives/header/header-notification/header-notification.html',
restrict: 'E',
replace: true,
link: function (scope) {
scope.authService = authService;
}
}
}]);
My html is as
<li data-ng-hide="authentication.isAuth">
I really feel I am just doing this wrong. Any help would be greatly appreciated.
Upvotes: 0
Views: 496
Reputation: 4208
what is authentication.isAuth
in your view.
I think you miss spelled your object.
<li data-ng-hide="authService.isAuth">
Your scope object is authService
not authentication
, right?
Update - Pass veraible to directive
I am assuming that you have your auth variable in your controller.
$scope.myAuthService = authservice;
Noe you can pass this variable to your directive as an attribute.
<header-notification my-auth="myAuthService"> </header-notification>
Here myAuthService
is a scope variable.
Change your directive to accept this variable,
app.directive('headerNotification', function () {
return {
templateUrl: 'app/scripts/directives/header/header-notification/header-notification.html',
restrict: 'E',
scope : {
myAuth : '=' // here you specify that you need to convert your attribute variable 'my-auth' to your directive's scope variable 'myAuth'
},
replace: true,
link: function (scope, element, attr, controller) {
// here you will get your auth variable
scope.myAuth; // this contains your auth details
}
}
});
Upvotes: 1