AngularJS和CodeigniteR语言 组合和数据传输



我最近开始学习AngularJS,我正在考虑创建一个应用程序,使用codeigniter作为后端(作为API将数据插入、更新和删除到MySQL数据库),AngularJS作为前端框架。所以我的问题是:我该如何做到这一点?我会在两者之间传输数据吗?

我想通过例子了解一些细节,因为我找不到一个好的视频教程将两者结合在一起。(找到了一些关于laravel和angular、Ruby on rails和angular的教程,但还没有真正深入到这些教程中)。如果有人知道一个很好的视频教程,甚至一篇博客文章来解释这一点,请提供一个链接。

在GitHub上发现了一些组合项目,但没有任何解释是什么以及如何完成的,它们并没有真正的用处。

我唯一知道的是,我必须以json的形式返回数据,但我不确定如何做到这一点。

谢谢!

CodeIgniterAngularJS的组合将帮助您构建新的HTML5应用程序

与JQuery不同,AngularJS是一个前端框架,它依赖于来自后端的数据,来自前端的所有通信都通过控制器方法进行,Angular中有getpost的操作。

CodeIgniter将充当API,它将向Angular控制器输出json响应。

我相信json_encode(data)会输出所需的JSON字符串,当前端收到该字符串时,Angular的数据表示层会处理这些事情,或者如果您想对数据执行任何操作,Angular也可以这样做。

我没有这个组合的任何链接,因为大多数人都转向了RubyonRailsAngularJS组合,担心CodeIgniter的新版本停止

很遗憾没有任何令人满意的链接/博客文章。如果时间允许我进行概念验证,我将非常乐意发布链接。

希望这能有所帮助。

编辑

JSON-

    [
        {"title": "t1"},
        {"title": "t2"}
        ....
     ]

HTML

 <body ng-app="app">
   <div ng-controller="MsgCtrl">
    <ul>
      <li ng-repeat="m in msg">{{m.title}}</li>
    </ul>
   </div>
 </body>

JS-

 var app = angular.module("app", []);
 app.controller("MsgCtrl", function($scope, $http) {
    $http.get('/index.php/ctrlname/methodname').
    success(function(data, status, headers, config) {
     $scope.msg = data;
    }).
    error(function(data, status, headers, config) {
     // log error
    });
 });

更新

对于插入、删除和更新,请使用CodeIgniterAngularJS

CodeIgniter控制器

class Msg extends CI_Controller {
    public function retrieveall() { .. } // Retrieves all Content from the DB
    public function create(){ .. } // Inserts the given data to DB
    public function retrieve($id){ .. } // Retrieves specific data from the DB
    public function update($id, $title){ .. } // Updates specific data from the DB
    public function delete($id){ .. } // Deletes specific data from the DB
    ...
}

CodeIgniter路由

$route['m'] = "msg";
$route['m/(:any)'] = "msg/$1";

HTML

<body ng-app="app">
   <div ng-controller="MsgCtrl">
    <ul>
      <li ng-repeat="m in msg">
           {{m.title}}
           <a href="#" ng-click="delete(m.id)">Delete</a>
           <a href="#" ng-click="edit(m.id)">Edit</a>
      </li>
    </ul>
    <input type="text ng-model="create.title">
    <button type="submit" ng-click="formsubmit"> Submit </button>
     <input type="text ng-model="editc.title">
    <button type="submit" ng-click="editsubmit(editc.id)"> Submit </button>
   </div>
 </body>

JS

var app = angular.module("app", []);
 app.controller("MsgCtrl", function($scope, $http) {
    $http.get('/index.php/m/retrieveall').
    success(function(data, status, headers, config) {
     $scope.msg = data;
    }).
    error(function(data, status, headers, config) {
     // log error
    });
    $scope.delete = function($id) {
        $http.get('/index.php/m/delete/' + $id).
        success(function(data, status, headers, config)       {
        $scope.result = data;
    }
    $scope.edit = function($id) {
        $http.get('/index.php/m/retrieve/' + $id).
        success(function(data, status, headers, config)       {
        $scope.editc = data;
    }
    $scope.editsubmit = function($id) {
       $http.get('/index.php/m/update/' + $id +'/' + $scope.editc.title).
        success(function(data, status, headers, config)      {
        $scope.result = data;
    }
    }
    $scope.formsubmit = function($id) {
               $http.post('/index.php/m/create', {data: create}).
                success(function(data, status, headers, config)      {
                $scope.result = data;
         }
     }
 });

我相信这会帮助你理解。这是一个简单的示例

最新更新