简体   繁体   中英

How to include date/timestamp with each new entry?

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 -->

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM