CodeIgniter 和 AngularJS 的组合将帮助您构建新的 HTML5 应用程序。
与 JQuery 不同,AngularJS 是一个前端框架,它依赖于来自后端的数据,来自前端的所有通信都是通过 Controller Methods 进行的,还有 get 的操作 和 post 在 Angular 中。
CodeIgniter 将充当 API,它将向 Angular 控制器输出 json 响应。
我相信json_encode(data) 会输出所需的 JSON 字符串,在前端收到该字符串后,Angular 的 数据呈现 层会处理这些事情/或如果你想对数据执行任何操作,Angular 也可以做到。
我没有这个组合的任何链接,因为大多数人已经转向 Ruby on Rails 和 AngularJS 组合,担心 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
});
});
更新
使用 CodeIgniter 和 AngularJS
进行插入、删除、更新
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;
}
}
});
我相信这会帮助您理解。这是一个简单的例子