AngularJs With CodeIgniter PHP Framework

Hi i am going to explain you how can you integrate angularjs with codeigniter framework. In this article you can get idea to send and retrieve data using angularjs with codeigniter framework.
Step 1 – Create One Controller with action and return from action json data.

<?php
class MasterController extends CI_Controller {

  function __construct() {
    parent::__construct();
  }

   
  function index() {
    $data['pagetitle'] = ‘Page for Angular JS';
    $data['viewname'] = 'index';
    $this->load->view('master',$data);
  }
   
  function json_get_user() {
    	$arr = array(
	array( “name" => “smith", “age" => “20“, “city" => “adelade", “country" => “australia”),
         array(“name" => “john", “age" => “20“, “city" => “parth", “country" => “australia“ ),
	array(“name" => “david", “age" => “20“, “city" => “london", “country" => “england“ )
	);
         echo json_encode($arr);
 }

}
?>	

Step 2 – JS Script get Json Data For View

var App = angular.module('myApp', ['ui.bootstrap']);
function app_home($scope, $http) {
  $scope.name = ‘Amit';
   
  // Initialising the variable.
  $scope.users = [];
   
  // Getting the list of users through ajax call.
  $http({
    url: base_url + '/masterController/json_get_user',
    method: "POST",
  }).success(function (data) {
    $scope.users = data;
  });
}

Step 3 -Represent View with JSON Response

<html>
<body ng-app=“myapp”>
<div class="span6" ng-controller=“app_home">
   <p>My name is <em>{{name}}</em></p>
   <table class="table table-striped table-condensed table-bordered">
    <tr>
      <th>Name</th><th>Age</th><th>City</th><th>State</th>
    </tr>
    <tr ng-repeat="user in users">
      <td>{{user.name}}</td><td>{{user.age}}</td><td>{{user.city}}</td><td>{{user.country}}</td>
    </tr>
  </table>
</div>
<script src=“apphome.js”></script>
</body>
</html>

 Result On Html Look Link this : 

Name Age City Country
smith 20 adelaide australia
john 20 perth australia
david 20 lord england

 

  • Sam

    please share angular2 tutorial with codeigniter

  • Sam

    i need that

  • Good article…

  • Thank you

  • mahrizal

    Thanks a lot

  • Anil Mhaske

    hiii can u show edit functionality on model