从一个PHP文件的JSON数据为角范围 [英] GET json data from a php file for an Angular scope
问题描述
我想从一个PHP文件的JSON数据到一个角控制器中使用。林呼应 json_en code(pg_fetch_assoc($结果));
PHP文件中,当I 的console.log($ scope.contents) ;
的角度控制其带回JSON数据,但它回来的空当我尝试做一个 NG-重复
controllers.js
myApp.controller('ContentCtrl',函数($范围,$ HTTP){
$ HTTP({方法:GET,网址:'content.php'})成功(功能(数据){。
$ scope.contents =数据;
});
});
content.php
< PHP
require_once(sdb.php);
$结果= pg_query($ dbconn,SELECT * FROM内容ORDER BY名ASC);
回声json_en code(pg_fetch_assoc($结果));
的index.php
<!DOCTYPE HTML>
< HTML LANG =ENGT&;
< HEAD>
<链接rel =stylesheet属性HREF =CSS / style.css文件/>
< /头>
<机身NG-应用=对myApp>
< DIV NG控制器=ContentCtrl>
< UL>
<李NG重复=,在内容的内容>
&所述; A HREF =#> {{content.name}}&下; / A>
< /李>
< / UL>
< / DIV>
< /身体GT;
<脚本SRC =JS / jQuery的-1.10.2.js>< / SCRIPT>
<脚本SRC =JS / angular.min.js>< / SCRIPT>
<脚本SRC =JS / controllers.js>< / SCRIPT>
< / HTML>
您将要实际发送数据作为JSON。要做到这一点,你只需要添加标题(内容类型:应用程序/ JSON');
你的前回声
语句。所以content.php变为:
< PHP
require_once(sdb.php);$结果= pg_query($ dbconn,SELECT * FROM内容ORDER BY名ASC);标题(内容类型:应用程序/ JSON');
回声json_en code(pg_fetch_assoc($结果));
顺便说一句,有可能要与你的控制器做了几件事情。我会改变这样的:
myApp.controller('ContentCtrl',函数($范围,$ HTTP){
$ HTTP({方法:GET,网址:'content.php'})成功(功能(数据){。
$ scope.contents =数据;
});
});
这样:
myApp.controller('ContentCtrl',['$范围,$ HTTP',函数($范围,$ HTTP){
$ http.get('content.php')
.success(功能(数据){
$ scope.contents =数据;
});
}]);
额外的'$范围,$ HTTP,
前的函数定义可以让你在未来的运行如下,和不用彷徨
只是个人preference,但我认为这是更清洁的阅读。
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.
这篇关于从一个PHP文件的JSON数据为角范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!