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

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

问题描述

我在 Mac 上使用带有本地主机的 AngularJS 1.0、PHP 和 mysql.

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

我有一个非常简单的 mysql 数据库:cats".它有一张桌子,叫做小猫".该表有两列,id"和name".数据库中有三只小猫,名字分别是 Larry、Curly 和 Moe.

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.

现在,我有一些简单的 PHP 代码可以打开数据库,获取三个记录,然后以 JSON 格式返回它们,如下所示:[{"id":"1","name":"Larry"},{"id":"2","name":"Curly"},{"id":"3","name":萌"}]

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

我使用了最新的 angular-seed 并在 Eclipse 中建立了一个项目.在 app.js 我写了这个:'使用严格';angular.module('Cats', ['ngResource','Cats.filters', 'Cats.services', 'Cats.directives'])我将我的 PHP 代码放在 services 文件夹中,并将其命名为index.php".如果我使用 Mac 上的 localhost 在浏览器中导航到该 services/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 项目在我的主索引页面上显示该 Cats 表的内容.纯旧文本,没有表格,只需绑定到可用资源并显示即可.

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.

提前致谢.

推荐答案

这也应该有效.代码行数明显减少,但请注意,已删除任何错误处理:

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'];

通常我会在 $resouce 上使用内置的 .get() 方法,但您的响应是数组的形式,.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.

您可以在此处找到关于 $resource文档

You can find the documentation on $resource here

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

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