用我的PHP服务连接AngularJS到MySQL? [英] connect AngularJS to mysql using my PHP service?

查看:158
本文介绍了用我的PHP服务连接AngularJS到MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS 1.0,PHP和MySQL与本地主机上我的Mac。

I am using AngularJS 1.0, PHP, and mysql with localhost on my Mac.

我有一个非常简单的mysql数据库:猫。它叫小猫一表。该表有两列,ID和名称。有三只小猫在名称数据库拉里,卷发,和莫伊。

I have a very simple mysql database: "cats". It has one table called "kittens". The table has two columns, "id" and "name". There are three kittens in the database with names Larry, Curly, and Moe.

现在,我有这将打开与dBASE一些简单的PHP code,得到三个记录,并返回他们在JSON是这样的:
[{ID:1,名:拉里},{ID:2,名:卷毛},{ID:3,名: 萌}]

Now, I have some simple PHP code which opens the dbase, gets the three records, and returns them in JSON like this: [{"id":"1","name":"Larry"},{"id":"2","name":"Curly"},{"id":"3","name":"Moe"}]

我使用了最新的角种子,在Eclipse中设置的项目。在app.js我写了这一点:
使用严格的;
angular.module('猫',['ngResource','Cats.filters','Cats.services','Cats.directives'])
我把我的PHP code。在服务文件夹,并把它命名为的index.php。如果我使用Mac localhost上导航到服务/ index.php文件在我的浏览器,我回来了JSON以上。到目前为止,一切都很酷。

I used the latest angular-seed and set up a project in Eclipse. In app.js I wrote this: 'use strict'; angular.module('Cats', ['ngResource','Cats.filters', 'Cats.services', 'Cats.directives']) I put my PHP code in the services folder and named it "index.php". If I navigate to that services/index.php in my browser using localhost on the Mac, I get back the JSON above. So far everything is cool.

现在 - 所有我想要做的(著名的临终遗言!;)是连接到PHP服务我已经和使用我AngularJS项目我的主索引页上显示,猫表的内容。普通的文本,没有桌子,就绑定到可用的资源,并显示它。

Now - ALL I WANT TO DO (!famous last words ;) is connect to the PHP service I have and display the contents of that Cats table on my main index page using my AngularJS project. Plain old text, no tables, just bind to the available resource and display it.

我已经尝试了演示各种形式的在线(许多是过时了)。谁能告诉我一个简单的,目前的方式做到这一点?对于我的生活中,我有各种AngularJS演示工作,但他们没有做的方式,我可以理解这种简单的任务。

I have tried all manner of demos online (many are out of date). Can anyone show me one simple, current way to do this? For the life of me I have all kinds of AngularJS demos working but none of them does this simple task in a way I can understand.

感谢您提前。

推荐答案

这也应该工作。这是code的显著更少的行,但要注意,任何错误处理已被删除:

This should also work. It's significantly fewer lines of code, but note that any error handling has been removed:

function FetchCtrl($scope, $resource) {
  var services = $resource('../services/index.php');
  $scope.data = services.query();
}
FetchCtrl.$inject = ['$scope', '$resource'];

通常我会用内置的获得()的方法 $种源,但你的反应是在一个阵列,其中 .query()默认支持的形式。

Normally I would have used the built in .get() method on the $resouce but your response is in the form of an Array, which .query() supports by default.

您可以找到关于 $资源文档这里

You can find the documentation on $resource here

这篇关于用我的PHP服务连接AngularJS到MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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