1

hello everyone i have some trouble returning a result from a function , i made a select input with ng-change the result of ng-change is used by an API then displays the result in a table the issue is i can't use the result out of the function

Here's my html page

        <select ng-model="dvdv"  ng-change="update()" class="form-control" >
        <option value="" disabled selected>Veuillez sélectionner un DVD</option>
        <option  ng-repeat="d in dvd"  value="{{d}}"> {{d}} </option>
        </select>

The table were data should be displayed

<table class="table table-bordered" >
        <thead><tr class="infoti" >
        <th>Id Dev</th>
        <th>Nom Dev </th>
        <th>Nom Ecu</th>
        <th>Etat</th>
        <th>Action</th>
        <tr>
        </thead>
        <tbody>     
   <tr dir-paginate=" dev in devs | itemsPerPage:7| filter: search">
            <td>{{dev[0]}}</td>
            <td>{{dev[1]}}</td>
            <td>{{dev[2]}}</td>
            <td>Non Validé</td>
      <td><button class="btn btn-default">Validé</button></td>
           </tr>
</tbody>
</table>

and this is the Function am using to get the data from select input by ng-change and am consuming

$scope.devs=[[]]
$scope.update=function(){

  console.log($scope.dvdv)
  $http.get("/dev?nommag="+$scope.dvdv)
    .success(function(data){
      $scope.devs = data ;
      console.log($scope.devs)
    })
}

here's a pic to explain how i want it to work

enter image description here

when i select an option i get the data then displayed but it's not printed in the table below else it work for a static so there's no issue with the syntax or ng-repeat

Kamel Mili
  • 1,374
  • 3
  • 19
  • 43
  • `$http.get()` is an asynchronous call. When you print `$scope.devs` outside the request is empty because it prints before the call has finished. – ddepablo Apr 11 '16 at 14:32
  • so if i can't use it outside how can i display the data !! – Kamel Mili Apr 11 '16 at 14:41
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – The Reason Apr 11 '16 at 15:08
  • @KamelMili You can use the data because when it is available and you update `devs` angulars data binding takes over and updates the view. On a side note `.success()` is deprecated. Use `.then()` instead. – ste2425 Apr 11 '16 at 15:10
  • @ste2425 it's still don't work :/ – Kamel Mili Apr 11 '16 at 15:17
  • Does anyone has an idea about $timeout i think mayebe itis the solution – Kamel Mili Apr 11 '16 at 16:12
  • Can you elaborate on the parameter passed to filter filter: search. Where is this object created. I guess it is empty and that is causing the table to be empty – Vipul Apr 12 '16 at 11:57
  • no am passing it in the input ng-model="search" – Kamel Mili Apr 12 '16 at 12:34

1 Answers1

-1

If by 'use' you do mean console.log(), you must do in inside the success callback (as @ddepablo suggests).
If instead by 'use' you do mean "show it in the html page", you simply put a

{{ devs }}

inside your html page, and angular will replace it with $scope.devs value...

MarcoS
  • 17,323
  • 24
  • 96
  • 174