Hina Vaja Hina Vaja - 2 years ago 139
JSON Question

using angular js get data from codeigniter controller

I work with codeigniter with angular js.
I had created one controller name Car.one model name car_model.
In car controller i have created one method view () in which i am fetching data from the table that code file is

Car Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car extends CI_Controller {

public function __construct() {


public function view()
$data = $this->db->get("cars")->result_array();
echo json_encode($data);

i want to fetch data in json formate so had fetched that data and echo json_encode($data);

now i want to retrieve that data in angualrjs.

add_car View

<!DOCTYPE html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<body ng-app="myapp" ng-controller="myctrl">
<form method="post" ng-submit="add(car_det)">
Car name : <input type="text" ng-model="car_det.cname" name="name"><br>
Car Image : <input type="file" custom-on-change="uploadFile" ng-model="car_det.cimage"><br>
<input type="submit" name="add">
<tr><td>ID </td><td>Car Name</td><td>Car Image</td></tr>
<tr ng-repeat="x in otherdata">

<script type="text/javascript">
var app = angular.module("myapp",[]);
$scope.otherdata = response.data;


Error :

While i writing this code in my file, problem is that it shows data in table format it is proper but also showing extra json array in starting of page.

I am getting this output :


enter image description here

i had not written any code to display in starting of page still showing that ...

how to resolve that problem .. ?

Answer Source

@Hina I Found that ans by another way

I found your answer change your way of thinking otherside use other concept like session.. follow below code :

In Controller :

public function view()
      $data = $this->db->get("cars")->result_array();

In add_view file

   $scope.otherdata = <?=$this->session->userdata("records")?>;

Try This Your problem will be solved .... :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download