Yevgeniy Bagackiy
Yevgeniy Bagackiy

Reputation: 974

How to print value from an array in angular js

I have select option code:

<select ng-init="loadHouse()" name="house" ng-model="house" class="form-control">  
 <option value="">Select House</option>  
 <option ng-repeat="house in houses" value="{{house.hid}}">{{house.hname}}</option>  
</select> 

Script:

$scope.loadHouse = function(){
 $http.get("Unity/load_house.php")  
 .then(function(data){
  $scope.houses = data;  
  console.log(data);
 })  
}

And load_house.php

include('../config/config.php');
require 'model/model.unity.php';
$insertHouse = loadHouses();
while($row = mysqli_fetch_array($insertHouse)){  
  $output[] = $row;  
}  
echo json_encode($output);

It seems to work and I am able to print data inside console. But inside option its empty:

enter image description here

What can be the problem?

EDIT Here is console data:

enter image description here

Upvotes: 2

Views: 862

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222720

You need to use data.data

$scope.loadHouse = function(){
 $http.get("Unity/load_house.php")  
 .then(function(data){
  $scope.houses = data.data;  
  console.log(data);
 })  
}

also use ng-options instead of ng-repeat as follows

<select ng-options="item as item.hcolor for item in houses" ng-model="selected"></select>

Upvotes: 1

Praveen Soni
Praveen Soni

Reputation: 780

you should define scope of houses, outside of loadHouse function try this.

$scope.houses = [];
$scope.loadHouse = function(){
  $http.get("Unity/load_house.php")  
   .then(function(data){
     $scope.houses = data;  
     console.log(data);
   })  
}

and your ng-model of select should different then ng-repeat local scope of house, try this

ng-repeat="_house in houses"

Upvotes: 0

Related Questions