Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: Condition statement based on current_user

There's something I've been stuck on for a couple days now. I would like to display an item based on the current user (id).

For an example:

<!-- if current user has already 'liked' this post -->
    Show 'UnLike' button
<!-- else -->
    Show 'Like' button
<!-- end -->

How do you use conditional statements based on the current user's id in Angularjs?

I'm using Rails as my backend along with Devise, if it helps..

like image 502
goo Avatar asked Jul 13 '26 19:07

goo


1 Answers

Lets make it easy.

First, setup gon.

At your Rails controller level, wherever needed, or in before_filter (before_action in Rails 4), do:

gon.current_user = { id: current_user.id } 

then on Angular side:

app.service 'sessionService', [ '$window', ($window)->

  factory =
    current_user: ->
      $window.gon.current_user

  factory
]

Then inject you session service and you'll have access to the current_user object via the method

like image 165
apneadiving Avatar answered Jul 16 '26 14:07

apneadiving



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!