0

In the success() callback of an $http.post() call, I am attempting to update an HTML table:

.success(function (data) {
          $scope.categories.push(data);
      });

HTML:

<tbody>
   <tr ng-repeat="category in categories">
    <td>{{category.Id}}</td>
    <td>{{category.CategoryTypeID}}</td>
    <td>{{category.IsContentLibraryItem}}</td>
    <td>{{category.Title}}</td>
    <td>{{category.CreatedBy}}</td>
    <td>{{category.CreatedDate}}</td>
   </tr>
  </tbody>

The result, however, is an empty row at the bottom of the table, and I believe this is because the success() function is executing before the post() function is complete. The new data isn't finished being written before success() is pushing a new (empty) row to the HTML table.

I need some way of delaying the $scope.categories.push(data); line until the post() is truly complete. I seem to recall that with jQuery/Ajax there was an onComplete() event or something like that, but is there an equivalent in AngularJS?

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
JMax2012
  • 373
  • 2
  • 6
  • 24
  • what happens if you inject '$window' and do "$window.alert("got new data"); ? – vidalsasoon Dec 02 '14 at 18:19
  • it behaves the same as it does without $window - the alert pops up, but the Firebug console shows an inprog/$digest error. – JMax2012 Dec 02 '14 at 18:29
  • .success() is only called when the promise successfully completes. Try console.log(data) in your success function call. It might really be empty. If you provided more code I could help more – UnbrandedTech Dec 02 '14 at 20:26
  • I'm beginning to think it really is empty. The console.log(data) produces an empty string. Maybe I need to dig back into the MVC controller itself. – JMax2012 Dec 02 '14 at 20:45
  • still don't understand the inprog $digest error ... the AngularJS documentation says that error means that an operation is already in progress – JMax2012 Dec 02 '14 at 20:51

1 Answers1

0

I think it's more of a design problem as it seems your controllers are trying to behave like services. If they were services they could be injected where it would be easier for your controllers to manage the data they are returning.

vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
  • Well, I'm brand new to AngularJS, and this code is my first attempt to stitch something together from various code samples, so if you know of a better construction than what I'm using, I'm all ears. Seems like doing an http post() followed by a get() to refresh data should be pretty simple, though. – JMax2012 Dec 02 '14 at 18:33
  • I think the discussion here applies to your problem: http://stackoverflow.com/questions/21919962/angular-share-data-between-controllers – vidalsasoon Dec 02 '14 at 19:23
  • It's a timing issue. I need to update the question to reflect this. – JMax2012 Dec 02 '14 at 19:51