我在使用带有codeigniter的角度js时遇到了问题。我没有收到任何错误,但数据没有显示出来。我正在尝试学习如何创建一个带有angularjs和Codeigniter的RESTful应用程序,如果有人有任何例子随时发送给我一个链接。
CI控制器
<?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);
}
}
?>
CI视图
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
</head>
<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="<?=base_url()?>app/homeapp.js"></script>
</body>
</html>
homeapp.js
var App = angular.module('myApp', []);
function app_home($scope, $http) {
$scope.name = 'John doe';
// 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;
});
}
答案 0 :(得分:0)
您尚未在app模块中添加功能app_home作为控制器。
//如下所示。注意,尚未验证语法
var App = angular.module('myApp', [$scope]);
App.controller('HomeController', ['$scope','$http', app_home($scope, $http)]);
function app_home($scope, $http) {
$scope.name = 'John doe';
// 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;
});
}