0

If I switch from the hard-coded items list in the following jsfiddle http://jsfiddle.net/rubyoma/bFJZK/ using a plain $http

$http.get('/mypath.json').then (response) =>
  response.data)

The data is async and come back after the table is rendered. I don't need to listen for changes, all needed is the json and search/sort client side (as it works right now). How would you make this work with the http get?

Post-accept EDIT: For tables I'd check out http://lorenzofox3.github.io/smart-table-website too

RAS
  • 8,100
  • 16
  • 64
  • 86
oma
  • 38,642
  • 11
  • 71
  • 99

1 Answers1

1

As I understand, you want to replace the existing values in $scope.items with a $http request that fetches the values.

First - move the hard coded entries into the JSON file.
Second - remove the hard coded item entries in the controller.
Third - Have the following http call:

$http.get('/path/to/your/json/file')
    .success(function (result) {
        $scope.items = result;
        //We now have the data with us. Prepare for display
        $scope.search();
    });

Fourth - Remove the $scope.search() at line 100 - you can see that it is now called after the items have been fetched.

That's it. You don't have to change anything else - No listener is needed. The moment the async call returns with the items, your code will prepare the items and display.

Plunkr that demonstrates the code.

callmekatootie
  • 10,989
  • 15
  • 69
  • 104
  • the plunkr shows unrelated things, 'Map syntax', 'string syntax'. Did your plunkr get overridden or did you forgot to save? I see the data.json that's relevant, but that's it. – oma Oct 04 '13 at 09:25
  • I think it got overridden... Are you still looking for a complete example? – callmekatootie Oct 04 '13 at 20:27
  • no need, it worked as you proposed! It redraws the table, I guess that's part of ng-repeat. A bit magical, but any new framework feels like that in the beginning. Thanks again, katootie, you rock! – oma Oct 05 '13 at 12:57