简体   繁体   中英

Calling a value from JSON in the controller in AngularJS so I can edit it

I am wondering if somebody can offer me help here.

I am working with a JSON file to create a timeline using AngularJS. I have been able to figure out how to output the date using the HTML binding ng-repeat loop, but I need to edit the date output; "2013-01-01" becomes "December 31, 2012" after I reformat it.

I have been struggling to figure out how to pull the date value IN THE CONTROLLER so that I can apply it in a function that will turn that value into the proper date -- by adding another day to the date as it is.

Thank you very much! I hope that made sense.

Javascript automatically localizes a date. The date filter in angular expects a timezone and when one is not provided assumes UTC. The issue is that once your timezone-less (thus assumed UTC) string gets localized, it is no longer precisely 2013-01-01, but however many hours shy of that your timezone is. Here in Pacific Standard Time, that's -8. So the date appears to be the day before.

Data from the server should have a timezone associated with it - that's just best practice. Without it, assuming UTC is a good bet. After all, some standard is needed.

Here's a Plunker showing a few examples of how this works, should you need them: http://plnkr.co/edit/Jhwnkj?p=preview

Use Angular date Filter in your view. You don't have to change any value in the controller. Here is the example for you http://plnkr.co/edit/iqIpW5uADTRVFycDxdBa

Read AngularJS date filter. http://docs.angularjs.org/api/ng.filter:date

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