Angular ng-options显示数据库中的数据

时间:2016-07-12 19:43:03

标签: angularjs json select options

  

编辑:代码没问题,问题是select select,无法使用动态角度元素,请使用类browser-default而不是input-field并且不要使用jquery进行无效化。

您好我试图在{ "records":[ { "numero":"312312" }, { "numero":"31221111" }, { "numero":"311241" }, { "numero":"112441" }, { "numero":"11312" }, { "numero":"131" } ] } 标记中显示带有角度的数据 我读了很多,但我无法解决这个问题。正如你可以看到测试代码的工作,但功能没有。需要帮忙。感谢。

这是PHP端点返回的JSON:

 app.controller('chequeoCtrl', function($scope, $http) {
   //with this function do not work
   $scope.leerNumero = function() {
     $http.get("objetos/autoelevador/leer_numero.php").success(function(response) {
       $scope.data = response.records;
       console.log($scope.data);
     });
   };
   $scope.leerNumero();

   // with this array works, just for test!!
   /*$scope.names = [{"name":"pepe"},{"name":"pepe2"}];
   console.log($scope.nombres); */
 })

控制器:

select

我的<select ng-model="autoelev" ng-options="item.numero as item.numero for item in data"> <option value="" disabled selected>Seleccionar autoelevador</option> </select> 代码:

var startX = 501;
var finalX = 320;
var multiply = .01;
var res = finalX - startX;
var extract = multiply * res;
var toEval;

if (res > 0) {
  toEval = "<=";
} else {
  toEval = ">=";
}
console.log("extract= " + extract + "");

while (eval(startX + toEval + finalX)) {

  startX += (-1.81);
  console.log(startX + " " + extract);

}

3 个答案:

答案 0 :(得分:1)

尝试以下代码,Demo here

查看:

<body ng-controller="MainCtrl">
  <select ng-options="item.numero for item in data" ng-model="chosen">
      <option value="" disabled selected>Seleccionar autoelevador</option>
  </select>
</body>

<强>控制器:

angular.module('app', [])
  .controller('MainCtrl', function($scope, $http) {
    $http.get('data.json')
      .then(function(res) {
        $scope.data = res.data.records;
        console.log($scope.data);
      });
  })

在控制器中执行$scope.data = response.data.records并确保其中包含一系列选项。

控制器中的

console.log($scope.data)应如下打印:

[{"numero":"312312"},{"numero":"31221111"},{"numero":"311241"},{"numero":"112441"},{"numero":"11312"},{"numero":"131"}

答案 1 :(得分:0)

根据$ http文档,您的响应对象有一个data属性,用于存储您的数据(https://docs.angularjs.org/api/ng/service/ $ http)

所以你应该使用

post 'sites_search_results' => sites_search_results#index

也不推荐使用成功函数。请使用

$scope.data = response.data.records;

答案 2 :(得分:0)

您可以使用angularjs从数据库中选择数据。

// Application module
var App = angular.module('myApp',[]);
App.controller("DbController",['$scope','$http', function($scope,$http){

// Function to get details from the database
getInfo();
function getInfo(){
// Sending request to sample.php files
$http.post('databaseFiles/sample.php').success(function(data){
// Stored the returned data into scope
$scope.details = data;
});
}

<强> Details.php。

<?php
// Including database connections
require_once 'database_connections.php';
// mysqli query to fetch all data from database
$query = "   ";//write your select query
$result = mysqli_query($con, $query);
$arr = array();
if(mysqli_num_rows($result) != 0) {
while($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
}
}
// Return json array containing data from the databasecon
echo $json_info = json_encode($arr);
?>