1

Im working on a project that uses AngularJS and Ionic. The project will have inputs that forms a list and anyone may comment on each list item.

How do I include a date/time that a user adds an list item or comments on it?

I done some research and understand that AngularJS has a existing item, kind of like a inbuilt method ("[ ].created") for this. I saw many projects just use that without any controllers. I tried but it didnt work for me. I tried to then add a js code, didnt work as well. My code below.

Not too sure if its a Ionic compatibility issue if any? Would appreciate some help. Sticking to the AngularJS method will be preferred! Thank you!

The AngularJS documentation for dates


The resultant code is as follows with a filter:

html portion (included ng-controller="MainCtrl")

<div class="post row" ng-repeat="post in posts"></div>

<a href="{{ post.url }}">{{ post.title }}
     <span class="url">({{ post.url | hostnameFromUrl }})</span>
</a>

<br> {{ post.created | date }}  
<!-- this is the angularJS code added -->
Thinkerer
  • 1,568
  • 4
  • 21
  • 40

1 Answers1

1

I'm not quite sure what are you tiring to achieve with this "$scope.post" variable

It will throw undefined because you are creating static object "$scope.post" without "get()" function here:

$scope.post = {url: 'http://', title: ''};

If you are tying to revive some data form server try using angular $http component

Here also "$scope.post[0]" and "post.getDate" will become undefined

$scope.date = post.getDate( $scope.post[0].created);
  • With reference to the codes here. 'https://github.com/deontologician/pipr/blob/master/tut.org' The author simply uses a {{ item.created | date }} to show the date and time stamp. I am trying to add the same to mine. – Thinkerer Oct 19 '14 at 11:23
  • in this tutorial "pips" are fetched from server https://docs.angularjs.org/api/ngResource/service/$resource and they contains property "created" This mean you have to save post creation time together with other data, for ex. `post = { id: '1', content: 'comeText', created: new Data() }` – Artur Kupiec Oct 19 '14 at 14:09
  • I see, thanks for the help. What do I need to insert into the "created: new Data()" portion in order for the timestamp to append to new entries? Attached is a plunkr so its easier to discuss :) http://embed.plnkr.co/q3GPfPiCHQDFT9Lblyxt/preview – Thinkerer Oct 19 '14 at 15:07
  • I was looking at Steven Levithans date time format here. [link](http://stackoverflow.com/questions/3552461/how-to-format-javascript-date). Other than adding `created:' '` into `post={}`, I also added `var now = new Date();` and `var time = dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");` to the submit controller. Still not working. – Thinkerer Oct 19 '14 at 15:56
  • Here you have working ex. http://plnkr.co/edit/sXZyfSufQYe9nElrvPEf and for date formatting you should use angular filter https://docs.angularjs.org/api/ng/filter/date – Artur Kupiec Oct 19 '14 at 16:45
  • Thanks @Artur, I realised I sent the wrong Plunkr. My factory now does not include the array. The array is instead within the submission page controller file under nav.js. After inserting your code...I got this. Dates not appearing though...no error. [PLUNKER LINK](http://plnkr.co/edit/lor2E51O6e5ZpvK8jBaY) – Thinkerer Oct 20 '14 at 14:49