使用Angular JS从Codeigniter控制器获取数据 [英] using angular js get data from codeigniter controller
问题描述
我使用angular js处理codeigniter。
我创建了一个控制器名称Car。一个模型名称car_model。
在汽车控制器中,我创建了一个方法视图(),其中我从表中获取代码文件为
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
汽车控制器 strong>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Car extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Car_model');
}
public function view()
{
$data = $this->db->get("cars")->result_array();
echo json_encode($data);
}
}
我想在json格式中获取数据,因此已获取该数据并回显json_encode($ data);
i want to fetch data in json formate so had fetched that data and echo json_encode($data);
现在我想在angualrjs中检索该数据。
now i want to retrieve that data in angualrjs.
add_car视图
<!DOCTYPE html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<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">
</form>
<br>
<table>
<tr><td>ID </td><td>Car Name</td><td>Car Image</td></tr>
<tr ng-repeat="x in otherdata">
<td>{{x.id}}</td><td>{{x.cname}}</td><td>{{x.cimage}}</td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var app = angular.module("myapp",[]);
app.controller("myctrl",function($scope,$http){
$http.get("<?=base_url()?>Car/view").then(function(response){
$scope.otherdata = response.data;
});
});
</script>
**
错误:
虽然我在文件中编写此代码,但问题在于它以表格式显示数据是正确的,但同时也显示了额外的json数组
我正在获得此输出:
**
我没有写过在页面开头显示的任何代码仍然显示...
i had not written any code to display in starting of page still showing that ...
如何解决该问题..?
推荐答案
如果视图页面是html页面,则必须定义全局变量 baseUrl
,在其中需要编写静态 base_url()
值
If your view page is a html page then you have to define a global variable baseUrl
where you need to write static base_url()
value like this
var baseUrl = 'http://localhost/yourProjectName/';
如果此页面为php页面,则定义一个隐藏的输入标签,该值为 base_url();
并在内部脚本中写入
If this your page is php page then defiine a hidden input tag and this value is base_url();
and inside script write
var baseUrl = $('#inputId').val();
然后您的脚本是这样的。
then your script like this..
app.controller("myctrl",function($scope,$http){
$http.get(baseUrl+'Car/view').then(function(response){
$scope.otherdata = response.data;
});
});
这篇关于使用Angular JS从Codeigniter控制器获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!