Angularjs - Correct way to send a variable to a pure JavaScript function and return to orderBy filter

StackOverflow https://stackoverflow.com/questions/18078280

Domanda

I'm not sure how to approach this issue.

I'm receiving a JSON from the server {createdOnT: "Sun Aug 04 10:27:39 IDT 2013"}. Now I would like to convert the value of the JSON to a javscript Date object. So in my controller I have a function that looks like this

$scope.getdate = function(date) {   
  var unformatedDate = date;
  var formated=unformatedDate.replace("IDT","");
  var angformat=new Date(formated);
  $rootScope.date = angformat;
  return $rootScope.date;
} 

In my view I call the function like this

<span ng-class="getdate(val.createdOnT)">
  <b>Date: &nbsp</b>{{date| date:'medium'}}
</span>

And finally try to orderBy like this

<li ng-repeat="(key, val) in JSON | orderBy:'-date'">

Finally I would like to send the converted value to the DOM and use angular's orderBy filter to order the results in a descending order. My guess is that something is wrong with my flow, because I'm trying to use the orderBy filter before the getdate function converted the dates...

Thanks ahead, Gidon

È stato utile?

Soluzione 2

So, I discovered that the JSON that I was receiving from the server was an Object, and not an Array. That is why the orderBy function did not work, and also why the data in the view was being presented in an un-ordered fashion.

Altri suggerimenti

What you attempting doesn't look quite right to me.

You are passing the result of getdate into the ng-class directive - all this will accomplish is that will apply css class with result of the function to the span. If you check the resultant html in something like chrome, you will see this to be the case.

What you should perhaps do instead, is in your controller, loop through your returned data and apply the function to the correct field and store the result back into it, OR

apply the function directly on the filter so it is orderby: 'getdate(val.createdOnT)'

The last option is probably what you want to do. See

http://docs.angularjs.org/api/ng.filter:orderBy

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top