Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度ng选项显示数据库中的数据_Angularjs_Json_Select_Options - Fatal编程技术网

Angularjs 角度ng选项显示数据库中的数据

Angularjs 角度ng选项显示数据库中的数据,angularjs,json,select,options,Angularjs,Json,Select,Options,编辑:代码正常,问题是具体化选择,不使用动态角度元素,使用类浏览器默认值而不是输入字段,并且不使用jquery进行序列化 您好,我正在尝试在带有角度标记的选择标记中显示数据 我读了很多书,但我不能解决这个问题。正如您所看到的,测试代码可以工作,但函数不能。我需要帮助。谢谢 这是PHP端点返回的JSON: { "records":[ { "numero":"312312" }, { "numero":"31221111"

编辑:代码正常,问题是
具体化
选择
,不使用动态角度元素,使用类浏览器默认值而不是输入字段,并且不使用jquery进行序列化

您好,我正在尝试在带有角度标记的
选择
标记中显示数据
我读了很多书,但我不能解决这个问题。正如您所看到的,测试代码可以工作,但函数不能。我需要帮助。谢谢

这是PHP端点返回的JSON:

{
   "records":[
      {
         "numero":"312312"
      },
      {
         "numero":"31221111"
      },
      {
         "numero":"311241"
      },
      {
         "numero":"112441"
      },
      {
         "numero":"11312"
      },
      {
         "numero":"131"
      }
   ]
}
控制器:

 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  ng-model="autoelev" ng-options="item.numero as item.numero for item in data">
  <option value="" disabled selected>Seleccionar autoelevador</option>
</select>

自动升降器

根据$http文档,您的响应对象具有存储数据的数据属性($http)

所以你应该使用

$scope.data = response.data.records;
此外,不推荐使用success函数。请使用

 $http.get("objetos/autoelevador/leer_numero.php").then(
请尝试下面的代码:

查看:

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

您可以使用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;
});
}
<?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);
?>
Details.php.

// 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;
});
}
<?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);
?>


返回的数据似乎有问题,我尝试使用具有相同数据的本地函数,但使用get函数无效,我不知道为什么,当我使用consle.log时,两个数组都是相同的!尝试
$scope.data=JSON.parse(response.data.records)
。您的响应可以是字符串,而不是JSON.response.data.records i get TypeError:无法读取未定义的属性“3”,并且使用JSON.parse(response.data.records)我在Object.parse(native)btw的位置1处获得JSON中意外的标记o,使用$scope.data=response.records;我可以正常使用ng repeat。问题只在于ngOptions,它的SEM与外部数据有关。控制器中声明的数组工作正常。这是console.log显示的$scope.data=response.records;console.log($scope.data);[Object,Object,Object,Object,Object,Object]console.log($scope.data[3]);对象{numero:“112441”}请检查这张图片,我解释得稍微好一点,阅读评论