如何用角度js从数组中输出值

时间:2020-12-21 21:44:26

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

和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:

它似乎可以工作,我可以打印数据在控制台。但是里面的选项是空的:

如何用角度js从数组中输出值

What can be the problem?

有什么问题吗?

EDIT Here is console data:

这里是控制台数据:

如何用角度js从数组中输出值

2 个解决方案

#1


1  

You need to use data.data

您需要使用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

也可以使用ng选项,而不是ng-repeat。

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

#2


1  

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

你应该定义房屋的范围,在loadHouse函数之外试试这个。

$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模型的选择应该不同于ng-repeat局部范围的房子,试试这个。

ng-repeat="_house in houses"

#1


1  

You need to use data.data

您需要使用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

也可以使用ng选项,而不是ng-repeat。

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

#2


1  

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

你应该定义房屋的范围,在loadHouse函数之外试试这个。

$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模型的选择应该不同于ng-repeat局部范围的房子,试试这个。

ng-repeat="_house in houses"