0

所以我有点麻烦。我已经查看了从Injecting service 到 Directive的所有先前解决方案,但我真的不知道我做错了什么。我有一个如下所示的 authServices。

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;
}]);

我通过他们提出的方法注入它。

    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;
    }
    }
}]);

我的html是

    <li data-ng-hide="authentication.isAuth">

我真的觉得我做错了。任何帮助将不胜感激。

4

1 回答 1

1

你认为什么authentication.isAuth

我认为你错过了拼写你的对象。

<li data-ng-hide="authService.isAuth">

你的范围对象authService不是authentication,对吧?

更新 - 将验证传递给指令

我假设你的控制器中有你的 auth 变量。

$scope.myAuthService = authservice;

不,您可以将此变量作为属性传递给您的指令。

<header-notification my-auth="myAuthService"> </header-notification>

myAuthService是一个范围变量。

更改您的指令以接受此变量,

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
                }
            }
});
于 2015-11-11T05:59:07.083 回答