urish / angular-moment

Moment.JS directives for Angular.JS (timeago and more)
MIT License
2.6k stars 397 forks source link

Directive doesn't but should update when timestamp changes #177

Closed KurtSteinkraus closed 9 years ago

KurtSteinkraus commented 9 years ago

The directive should update its text when the timestamp (passed in via am-time-ago) changes, but it doesn't.

I think the problem is here: https://github.com/urish/angular-moment/blob/master/angular-moment.js#L257 This is scope.$watch() but should be attr.$observe() like the others? Making that small change fixes the problem for me.

I'm writing this issue rather than submitting a patch because it seems unlikely to me that I'd be the only one having this problem, so I'm wondering if I'm doing something wrong or non-standard. (FTR: I'm using Angular 1.4.)

urish commented 9 years ago

Hi, thanks for reporting!

We actually have a test case for this:

https://github.com/urish/angular-moment/blob/master/tests.js#L86

Can you please try to create a test case that reproduces the issue?

Thanks!

KurtSteinkraus commented 9 years ago

I think the problem is that I'm passing into the am-time-ago attribute not a scope variable name but rather the value itself. It's surprising that angular-moment (mostly) works when passing in a value, rather than a scope variable name, given that it's designed only for the latter!

My particular use case unfortunately doesn't lend itself to passing in the name of a scope variable. I'm passing in the value of calling a method on an object. That object gets updated by various other parts of my program.

Is there any way to pass in a value (i.e., to specify an expression as the directive attribute value) and have the directive auto-update when the expression's value changes?

urish commented 9 years ago

I see no reason why it wouldn't work. Can you try to create a test case of a plunker reproducing the issue?

Thanks!

KurtSteinkraus commented 9 years ago

Sure: http://plnkr.co/edit/KN5Lbo1ZSOw2y05sz7gj?p=preview

urish commented 9 years ago

Thanks, now I understand what you were referring to :)

am-time-ago is not supposed to be used with the curly braces, you can write any expression that you like without curly braces, e.g. <time am-time-ago="myCtrl.someFunction()"> should do the trick.

Does this help?

urish commented 9 years ago

Hi @KurtSteinkraus , I see that you have updated the plunker and it seems to be working there. Do you need any further discussion or can we close the issue?

Thanks!

KurtSteinkraus commented 9 years ago

It helps and fixes the problem I was seeing. I'm not sure why I put curly braces in the first place. I don't understand Angular very well... :-/ Thanks!