【发布时间】:2015-03-04 01:14:37
【问题描述】:
我正在使用 AngularJs 应用程序,通过 $http 使用 Slim PHP 使用 API REST(下一步:$resource,我知道)。当控制器这样编写时,从 API 检索数据时一切正常:
angular
.module("adminTaller", ['ngRoute'])
...
.controller("CustomersListController", ['$http', function($http){
var ctrl = this;
ctrl.customers = [];
$http.get('api/customers')
.success(function(data, status, headers, config){
ctrl.customers = data;
})
.error(function (data, status, headers, config) {
...
});
}]);
“客户”显示在此视图中:
<div ng-controller="CustomersListController as lstCtrl">
<input type="text" ng-model="searcher" />
<table>
<thead>
<tr>
<th>Apellido y Nombres</th>
<th>Domicilio</th>
<th>TE Celular</th>
<th>TE Comercial</th>
<th>TE Domicilio</th>
<th>Acciones</th>
</tr>
</thead>
<tbody ng-repeat="customer in lstCtrl.customers | filter:searcher | orderBy: fullname">
<tr>
<td>{{ customer.fullname }}</td>
<td>{{ customer.address }}</td>
<td>{{ customer.cellphone }}</td>
<td>{{ customer.businessphone }}</td>
<td>{{ customer.homephone }}</td>
<td><a href="#">Editar</a></td>
</tr>
</tbody>
</table>
但在我将 this 替换为 $scope 的那一刻:
angular
.module("adminTaller", ['ngRoute'])
...
.controller("CustomersListController", ['$http', '$scope', function($http, $scope){
var ctrl = $scope;
ctrl.customers = [];
$http.get('api/customers')
.success(function(data, status, headers, config){
ctrl.customers = data;
})
.error(function (data, status, headers, config) {
...
});
}])
,似乎什么都没有发生(我的意思是,表中没有加载“客户列表”),并且 Batarang 给了我一个奇怪的警告(如果 Batarang 被禁用,Chrome 的控制台什么也没说):
资源解释为脚本但以 MIME 类型传输 应用程序/x-js
我的 api/index.php 将 content-type 设置为 application/json:
$app = new Slim();
$app->response()->header('Content-Type', 'application/json');
$app->get('/customers', 'getCustomers');
$app->post('/customers', 'addCustomer');
$app->get('/customers/:id', 'getCustomer');
$app->put('/customers/:id', 'updateCustomer');
$app->delete('/customers/:id', 'deleteCustomer');
$app->run();
事实上,http 请求和 http 响应标头对我来说看起来不错:
Remote Address:[::1]:8080
Request URL:http://localhost:8080/ng-taller/api/customers
Request Method:GET
Status Code:200 OK
**Request Headers**
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:es-ES,es;q=0.8,en;q=0.6,pt;q=0.4
Connection:keep-alive
Cookie:__ngDebug=true; PHPSESSID=b8cedd49edcc461fda62208e66cfb150; b8cedd49edcc461fda62208e66cfb150=DEFAULT%7C0%7C2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55%2BUGpAo%3D%7C7456bf61db3500c8bb7b3bc38082a470ce4a2ad3
Host:localhost:8080
Referer:http://localhost:8080/ng-taller/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
**Response Headers**
Connection:Keep-Alive
Content-Length:1027
Content-Type:application/json
Date:Wed, 04 Mar 2015 00:26:12 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19
Set-Cookie:b8cedd49edcc461fda62208e66cfb150=DEFAULT%7C0%7C2M3TMlgUx3gTlaarYzHIdD28l8q9FTcNubt55%2BUGpAo%3D%7C7456bf61db3500c8bb7b3bc38082a470ce4a2ad3; path=/
X-Powered-By:PHP/5.4.19
当我使用 Batarang 检查视图的范围/模型时,它显示:
{
lstCtrl: { }
customers:
[ {
id: 2
createdAt: 03-03-2015
fullname: Abelardini, Antonio Miguel
address: Virasoro Nº2533, Piso 3º - Rosario
cellphone: 341 (15)562-4484
businessphone: 341 430-4574
homephone:
observations:
logs:
[ ]
errors: 0
} , {
id: 1
createdAt: 03-03-2015
fullname: Alvarez, Juan
address: Gallo Nº1254 (entre Nuria y Valdez) - Rosario
cellphone: 341 (15)485-6225
businessphone:
homephone:
observations: Puntual en el pago
logs:
[ ]
errors: 0
} , {
id: 4
createdAt: 03-03-2015
fullname: Blanco de Escalada, María de las Mercedes
address: Av. Álvarez Thomas Nº1542, Piso 11º Depto.'A' - Rosario
cellphone: 341 (15)326-5484
businessphone:
homephone:
observations:
logs:
[ ]
errors: 0
} , {
id: 3
createdAt: 03-03-2015
fullname: Tirado López, Juan Manuel
address: Costanera Nº12 (Bajada de los Pescadores) - Rosario
cellphone: 341 (15)675-1125
businessphone:
homephone: 3205 124-8586
observations:
logs:
[ ]
errors: 0
} ]
}
【问题讨论】:
-
如果要使用
$scope,只需使用$scope(无需分配给ctrl)。您还需要删除as lstCtrl并直接在 HTML 中引用customers。您可能应该阅读一下何时使用 controller as 表单以及与控制器范围有什么区别 -
谢谢@Phil,我会考虑的!你有什么建议我可以阅读更多关于它的信息吗?我当然对此感到困惑,Angular's controller documentation 没有解释它.. 我得到的消息来源也没有!提前致谢!
标签: json angularjs angularjs-scope mime-types