如何在角度js中打印数组中的值

时间:2018-02-07 07:58:03

标签: php angularjs

我有选择选项代码:

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

脚本:

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

和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);

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

enter image description here

可能是什么问题?

修改 这是控制台数据:

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要使用data.data

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

也可以使用ng-options代替ng-repeat,如下所示

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

答案 1 :(得分:0)

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

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

并且select的ng模型应该与ng-repeat的{​​{1}}本地范围不同,请尝试此

house