Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The Select element does get populated by the http request, but the options in the select element are not updating as the ng-model is changed. I am new to angularJs so im assuming its a simple fix. I have tried many variation with no positive results.

    <script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {    
$http.get('http://graph.facebook.com/platform')
.success(function(data) {
})
        });
</script>
<body ng-controller="MainCtrl">
  Search:
  <input type="search" ng-model="searchText" />

  <select>
    <option ng-repeat="item in data | filter: searchText">{{item}}</option>
  </select>
  <br>
     <pre>{{ data | json}}</pre>
</body>

http://plnkr.co/edit/C39yVDsG3OcfvwjVSxP9?p=preview

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

TarranJones, this Plunker should clear things up. Hard to give you a 100% clear answer without a look at your data. You should be able to figure it out from here.

http://plnkr.co/edit/UYm0SwtU6ePZkZLx2w2U?p=preview

To Answer your question in the comments I would replace:

app.controller('MainCtrl', function($scope) {
  $scope.colors = [
    {name:'black', shade:'dark'},
    {name:'white', shade:'light'},
    {name:'red', shade:'dark'},
    {name:'blue', shade:'dark'},
    {name:'yellow', shade:'light'}
  ];
});

With:

app.controller('MainCtrl', function($scope, $http) {

  $http.get('http://www.foo.com')
  .success(function(data) {
    $scope.colors = data;
  })
  .error(function() {
    console.log('My name is Error, now eat it!);
  });

});

Make sure to inject the $http.

Plunker here. http://plnkr.co/edit/UYm0SwtU6ePZkZLx2w2U?p=preview

UPDATE:

Tarran also ran into the problem of filtering a single returned JSON object from an API. Angular Filters can only accept arrays and so would not accept the object. In order for Tarran to filter the object he must first iterate through the object and store the object properties to an array. Once the array is then returned to the $scope you can filter the results. The plunker and code is provided below: http://plnkr.co/edit/9M3zZFN5jyV8w7fg7EE3?p=preview

Controller:

$http.get('http://graph.facebook.com/4') .success(function(data) {

    //CREATE AN EMPTY ARRAY 
    result = [];

    //ITERATES THROUGH THE OBJECT SAVING THE OBJECTS PROPERTIES TO ARRAY
    for (var i in data) {
      if (data.hasOwnProperty(i)) {

          //PUSHES THE PROPERTIES ONTO THE ARRAY
          result.push(data[i]);

      }
    }

    //SETS THE NEW DATASET TO THE ARRAY AND RETURNS TO $SCOPE
    $scope.dataset = result;

});

HTML:

  {{dataset}}

  <BR>
  <BR>Search:


  <input type="search" ng-model="searchText" />


  <BR>
  <BR>

  <select>
    <option ng-repeat="data in dataset | filter: searchText">{{data}}</option>
  </select>
share|improve this answer
1  
Hey great glad I could help. Mind checking me off as answering your question! –  rjm226 Dec 17 '13 at 17:21
 
iI have added $scope.data = {} to the FetchCtrl function. now I need to populate it with the data received from the http request. Do you know how i could pass data from a child to the parent function? –  TarranJones Dec 17 '13 at 17:24
1  
I updated the answer above with how I would proceed. –  rjm226 Dec 17 '13 at 18:03
 
Any chance your going to check off that I answered this one? –  rjm226 Dec 17 '13 at 19:24
1  
Tarran i've created a plunker that answers your questions, plnkr.co/edit/9M3zZFN5jyV8w7fg7EE3?p=preview. Make sure to check off that I answered this one. Ill also update the answer. –  rjm226 Dec 18 '13 at 20:02
show 4 more comments

Try to init your data as object before all.

$scope.data = {}

Or in HTML :

<div ng-controller="FetchCtrl" data-ng-init="data={}; fetch()">   
share|improve this answer
 
Of course, SCOPE. I have added $scope.data = {} to the FetchCtrl function. now I need to populate it with the data received from the http request. How do I pass data from a child to the parents function? hopefully thats the problem. –  TarranJones Dec 17 '13 at 17:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.