我尝试在Symfony2控制器中捕获错误,因为此代码显示:
public function indexAction($parent_id = 0) {
$response['message'] = "";
$breadcrumbs = array();
$response['entities'] = array();
if (!$entity) {
throw $this->createNotFoundException('No se encontraron grupos de detalles');
}
...
return new JsonResponse($response);
}
}
当!$entity
为TRUE时,如您所见,我会生成NotFoundException
,但我需要在AngularJS模板中向用户显示消息。这是模板的代码:
<ul id="nav-info" class="clearfix">
<li><a href="#/dashboard"><i class="icon-home"></i></a></li>
<li><a href="javascript:void(0)">Grupo de Meta-Detalles</a></li>
<li class="active"><a href="#/detailsgroup/list">Listar</a></li>
</ul>
<h3 class="page-header page-header-top">Grupo de Meta-Detalles <small>Listado.</small></h3>
<table id="example-datatables" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="span1"></th>
<th class="span1 hidden-phone">#</th>
<th><i class="icon-bookmark"></i> Nombre</th>
<th><i class="icon-bookmark"></i> Padre</th>
<th><i class="icon-bolt"></i> Descripción</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in MetaDetailGroup">
<td class="span1">
<div class="btn-group">
<a href="#/products/edit/{% verbatim %}{{ item.id }}{% endverbatim %}" data-toggle="tooltip" title="Editar" class="btn btn-mini btn-success"><i class="icon-pencil"></i></a>
<a href="#/products/delete/{% verbatim %}{{ item.id }}{% endverbatim %}" data-toggle="tooltip" title="Eliminar" class="btn btn-mini btn-danger"><i class="icon-remove"></i></a>
</div>
</td>
<td class="span1 hidden-phone">{% verbatim %}{{ item.id }}{% endverbatim %}</td>
<td><a href="javascript:void(0)">{% verbatim %}{{ item.name }}{% endverbatim %}</a></td>
<td><a href="javascript:void(0)">{% verbatim %}{{ item.parent }}{% endverbatim %}</a></td>
<td>{% verbatim %}{{ item.description }}{% endverbatim %}</td>
</tr>
</tbody>
</table>
我怎么能做到这一点?我的意思是如果没有什么可以显示(!实体)然后向用户显示消息,任何帮助或建议?
答案 0 :(得分:2)
我没有安装AngularJS,因此在JsFiddle上检查:)
public function indexAction($parent_id = 0) {
$response['message'] = "";
$breadcrumbs = array();
$response['entities'] = array();
if (!$entity) {
// Exception is replaced by message to your client-side script
$response['message'] = 'No se encontraron grupos de detalles';
}
return new JsonResponse($response);
}
调用symfony方法(GET或POST):
$http.get("/api/index/?parent_id=666", {})
.success(function(data, status, headers, config){
// Would be nice to check status/message and to hide if no error
$scope.ajaxResponse = data.message;
// TODO: Handle your entities
}).error(function(data, status, headers, config){
$scope.ajaxResponse = status;
});
在您的视图页面(HTML)中插入一些Angular JS变量
<tbody>
<tr><td colspan="5">{{ajaxResponse}}</td></tr>
<tr ng-repeat="item in MetaDetailGroup">
...