【问题标题】:Angular JS ng-repeat Can't getting Output with PHP arrayAngular JS ng-repeat无法使用PHP数组获取输出
【发布时间】:2015-03-10 00:48:46
【问题描述】:

我是 Angular JS 的新手,我想在 ng-repeat 中显示一个 php 数组。 这是我的html代码,

<div ng-controller="clientList">
 <ul>
   <li ng-repeat="client in clients">
    <h2>{{client.job}}</h2>
   </li>
 </ul>
 <p>Total number of phones: {{clients.length}}</p>
</div>

这是我的 Angular JS 控制器,

var anguApp = angular.module('anguApp', []);

anguApp.controller('clientList',function($scope,$http)
{

   $scope.clients=[];

   $http.get('customers.php').success(function (data, status) {
    $scope.clients = data;
    console.log(data);
   });
});

customers.php

<?php
$arr=['name'=>'David','job'=>'developer'];
header('Content-Type: application/json');
echo json_encode($arr);
?>

控制台日志: 在控制台中得到很好的响应。但没有进入浏览器。错误在哪里?....

【问题讨论】:

  • 你能在这里附上你的console.log(data)结果吗?

标签: javascript php html json angularjs


【解决方案1】:

看起来您正在从服务器请求一个没有对象的数组。在你的 html 代码中试试这个:

<li ng-repeat="client in clients">
    <h2>{{client}}</h2>
</li>

【讨论】:

  • 嗨,Gubkin,输出很好。但是我怎样才能用键打印一个特定的值。
  • 你不能......你正在从你的服务器获取一个这样的数组:['value1','value2','value3']。你的 php 数组需要看起来像这样: [{name: david}, {name: john}] 所以你可以按键重复它
  • 根据我对php的一点了解,试试这个: $named_array = array( "nome_array" => array( array( "foo" => "bar" ), array( "foo" => "巴兹")));
  • 没有得到输出。 :(
  • 嗯。对php不太熟悉。也许尝试像你的数组是这样的?:[{“name”:'john'},{'name':'david'}]。不知道这是否可行,反正谷歌应该不会太难
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多