您是否能够使用 AngularJs 将指令绑定到服务上的 getter 属性?
我有一个指令试图监视服务上的属性,它会在应用程序启动时更新一次。然后它永远不会更新,即使被监视的属性更新。
这是我的指令:
authorization.directive("authorization", ["$rootScope", "authenticationService", "authorizationService", function ($rootScope, authenticationService, authorizationService) {
return {
restrict: "A",
link: function (scope, element, attrs) {
var prevDisp = element.css("display");
function update(user) {
if (!authorizationService.isAuthorized(attrs.authorization, user)) { element.css("display", "none"); }
else { element.css("display", prevDisp); }
}
scope.$watch(authenticationService.user, function () { update(authenticationService.user); });
}
};
} ]);
这是我的服务的精简版:
App.factory("authenticationService", function ($http, $location) {
var _user = { email: null, ...... };
return {
get user() { return _user; }, //This is the property I am trying to bind too
login: function () { do stuff },
logout: function () { do other things }
}
});