从 Angular 范围的 php 文件中获取 json 数据 [英] GET json data from a php file for an Angular scope

查看:21
本文介绍了从 Angular 范围的 php 文件中获取 json 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 php 文件中获取 json 数据以在 Angular 控制器中使用.我在 php 文件中回显 json_encode(pg_fetch_assoc($result)); ,当我 console.log($scope.contents); 在角度控制器中时,它会带回json 数据,但是当我尝试执行 ng-repeat

时它返回空

controllers.js:

myApp.controller('ContentCtrl', function ($scope, $http) {$http({method: 'GET', url: 'content.php'}).success(function(data) {$scope.contents = 数据;});});

content.php:

index.php:

<html lang="zh-cn"><头><link rel="stylesheet" href="css/style.css"/><body ng-app="myApp"><div ng-controller="ContentCtrl"><ul><li ng-repeat="内容中的内容"><a href="#">{{content.name}}</a>

<script src="js/jquery-1.10.2.js"></script><script src="js/angular.min.js"></script><script src="js/controllers.js"></script></html>

解决方案

您需要实际将数据作为 JSON 发送.为此,您只需在 echo 语句之前添加 header('Content-Type: application/json'); 即可.所以 content.php 变成了:

顺便说一句,您可能想对控制器做一些事情.我会改变这个:

myApp.controller('ContentCtrl', function ($scope, $http) {$http({method: 'GET', url: 'content.php'}).success(function(data) {$scope.contents = 数据;});});

为此:

myApp.controller('ContentCtrl', ['$scope', '$http', function ($scope, $http) {$http.get('content.php').成功(功能(数据){$scope.contents = 数据;});}]);

在函数定义之前附加的'$scope', '$http', 可以让你以后缩小,.get 只是个人喜好,但我觉得读起来更干净.

I'm trying to get json data from a php file to use within an Angular controller. Im echoing json_encode(pg_fetch_assoc($result)); within the php file and when I console.log($scope.contents); in the angular controller it brings back the json data, but it comes back empty when I try doing an ng-repeat

controllers.js:

myApp.controller('ContentCtrl', function ($scope, $http) {
  $http({method: 'GET', url: 'content.php'}).success(function(data) {
    $scope.contents = data;
  });
});

content.php:

<?php
require_once("sdb.php");
$result = pg_query($dbconn, "SELECT * FROM content ORDER BY name ASC");
echo json_encode(pg_fetch_assoc($result));

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body ng-app="myApp">
 <div ng-controller="ContentCtrl">
   <ul>
     <li ng-repeat="content in contents">
       <a href="#">{{content.name}}</a>
     </li>
   </ul>
 </div>
</body>
   <script src="js/jquery-1.10.2.js"></script>
   <script src="js/angular.min.js"></script>
   <script src="js/controllers.js"></script>
</html>

解决方案

You'll want to actually send the data as JSON. To do that, you just need to add header('Content-Type: application/json'); before your echo statement. So content.php becomes:

<?php
require_once("sdb.php");

$result = pg_query($dbconn, "SELECT * FROM content ORDER BY name ASC");

header('Content-Type: application/json');
echo json_encode(pg_fetch_assoc($result));

As an aside, there are a few things you may want to do with your controller. I would change this:

myApp.controller('ContentCtrl', function ($scope, $http) {
    $http({method: 'GET', url: 'content.php'}).success(function(data) {
        $scope.contents = data;
    });
});

to this:

myApp.controller('ContentCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('content.php')
    .success(function(data) {
        $scope.contents = data;
    });
}]);

The additional '$scope', '$http', before the function definition allows you to minify in the future, and the .get is just personal preference, but I think it's cleaner to read.

这篇关于从 Angular 范围的 php 文件中获取 json 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆