2

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

5
  • how does your array look like console.log(data); Commented Feb 7, 2018 at 7:58
  • We would have to see what your response is, or how the house-object looks like, to be able to help you. Commented Feb 7, 2018 at 8:00
  • @Sajeetharan please see edit. Commented Feb 7, 2018 at 8:03
  • @zeropublix I edited the question. Commented Feb 7, 2018 at 8:03
  • @YevgeniyBagackiy btw instead of using an ng-repeat on your <option> check out ng-options. Commented Feb 7, 2018 at 8:05

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

im getting error when i use ng-options. Here is an error: angular.js:14525 Error: [$compile:ctreq] With ng-repeat its fine
that was just a suggestion
I found the problem. Thank you for help
How to add option "select House" in it?
|
0

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"

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.