Corner time with filter and $ http service

When the application loads, I register the filter and the service using angular. But the filter is trying to execute before the service returns with data, so my collection of elements barks in the filter. I had a violin, but I don’t know if you can use $ http in the violin because it is an external resource. Here is what I Fiddle Sorry that this actually doesn't work. it was a lot. I think my problem is time. I need a filter to wait for an HTTP response or just not use it.

The error I am getting now is "items is undefined", because a filter is applied there. I had this job before I tried to push the HTTP call into the service. But I feel this is an angular way, and I just want to be consistent. "

When my controller fires, it calls a call to retrieve the data:

eventApi.async().then(function () {
    $scope.eventCollection = eventApi.data();
});

but before it returns, the filter is applied in html:

 <tr ng:repeat="event in events  | myEventFilter:ddlFilter |
       orderBy:sort.column:sort.descending">

Fiddle

+5
source share
1 answer

Here is the fork of your updated violin that fixes a couple of missing dependencies ( eventApinot entered into the controller, $timeoutnot entered into the service eventApi). My comments will be based on this code: http://jsfiddle.net/BinaryMuse/zww7e/1/

, : " undefined" . , null/undefined . , - :

return function (items, eventFilterType) {
  var arrayToReturn = [];
  if (items === undefined) return arrayToReturn;
  // ...
}

; , ; , , $scope.eventCollection $scope.events if (live) . .

Angular: , $q, , , . , , :

// in the service
$timeout(function () {
  deffered.resolve(fakeEvents); // <-- resolve with the data
}, 2000);
return deffered.promise;

// in the controller
if (live) {
  $scope.events = eventApi.async();
}

, : http://jsfiddle.net/BinaryMuse/zww7e/2/

[]

, Angular - ; , :

fix ($ parse):

$parseProvider.unwrapPromises() getter/setter api, , . Angular , .

, Angular , ( , onces). $parseProvider.logPromiseWarnings(false).

promises undefined, .

, , - ( ) ( promises).

promises .

:

  • raw promises
  • .
  • - , .
  • IDE .

BREAKING CHANGE: $parse promises. , $parseProvider.unwrapPromises(true) api.

+8

All Articles