I'm trying to set a global object with user data (id, name, etc) and use it in a template to color comments. Like if the user is logged in, it highlights their comments.
I've tried this:
app.factory('UserService', function() {
return {
name : 'Johnny Williams',
id : 2
};
});
then I set the comment name to UserService.name and that works. BUT What I wan't to do is this:
var setcommentClass = function(comment) {
if (comment.name === UserService.name) {comment.type = 'user'}
else {comment.type = 'client'}
};
Then in the template use this:
ng-class="{user: comment.type == 'user' }
This doesn't work. Why is that? There's got to be a more eloquent way to do this. I don't want to store "comment.type" in the database because it is useless information. I've tried using:
ng-class="{user: comment.name == UserService.name }
but that didn't work either. I feel like that would be the easiest way. What is the protocol on using global information not tied to a scope and using it in the template itself? While also keeping user data secure.
If you need to refer the service within html document, you'll have to assign it to $scope
.
You can try:
$scope.UserService = UserService;
in your controller.