0

I'm trying to call an external scrip that gets data from a database, so I can then populate a select dropdown. I've tried this so far, but I get a load of empty li elements. It is the right count of what there should be just nothing showing?

My controller

app.controller('agenciesController', function($scope, $http) {
  var url = "/ajax/getAgencies.php";

  $http.get(url).success( function(response) {
    $scope.agencies = response; 
    $scope.agenciesArray = [];
    angular.forEach(response, function(value, key){
      $scope.agenciesArray.push(value);
    })
    console.log($scope.agenciesArray);
  });
})

My HTML

<body ng-controller="Controller">
   <div ng-controller="agenciesController">
      <ul ng-repeat="agencyName in agencies">
        <li>{{agenciesArray.agencyID}}</li>
      </ul>
   </div>
</body>

UPDATE - This code is working but not returning only one response but all.

  <div ng-controller="agenciesController">
    <ul ng-repeat="agencyName in agenciesArray">
      <li>{{agencyName}}</li>
    </ul>
  </div>

enter image description here

3
  • Please add sample response Commented Jun 22, 2016 at 10:45
  • agenciesArray is an array. agenciesArray.agencyID will be undefined. that is why you are gettting empty li. Commented Jun 22, 2016 at 10:47
  • Your ng-repeat loops on $scope.agencies but in the li you have agenciesArray.agencyID that I don't know what can be Commented Jun 22, 2016 at 10:50

2 Answers 2

4

agenciesArray is an array. agenciesArray.agencyID will be undefined. that is why you are gettting empty li.

Do you intend to do this?

<ul ng-repeat="agency in agenciesArray">
        <li>{{agency.agencyName}}</li>
      </ul>
Sign up to request clarification or add additional context in comments.

5 Comments

This is bringing stuff back but not just agencyName by its self check edit.
good one you have to check for the attribute agencyName in the object that you called agencyName in th eng-repeat
@rick so basically in ng-repeat it's like assigning a variable to agenciesArray
Just another question, how can I put these as <select> options instead of in a ul?
The syntax is "iterator in list"
0

Try this

<ul ng-repeat="agencyName in agencies">
    <li>{{agencyName.agencyID}}</li>
  </ul>

Reason:Because agencyArray is nothing in the given context

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.