Можете ли вы привязать директиву к свойству геттера на службе с помощью AngularJs?AngularJs Связывающие директивы к свойствам геттера
У меня есть директива, которая пытается посмотреть свойство на службе, оно обновляется один раз при запуске приложения. Тогда он никогда не обновляется, даже когда просматриваемые свойства обновляются.
Вот моя директива:
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 }
}
});
Вы пробовали использовать третий параметр '$ watch()'? Передайте true в качестве третьего параметра, а затем проверьте, запущен ли ваш '$ watch()' или нет ... – callmekatootie