2014-02-06 36 views
0

我試圖捕捉到的Symfony2控制器內部錯誤的消息,因爲這代碼所示:如何發送並顯示在AngularJS和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爲真,那麼當你看到我產生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

嘗試返回'返回新JsonResponse(陣列( '消息'=> '否本身encontraron grupos德detalles')) ;'而不是拋出異常 –

+0

@AleksejVasinov我可以做到這一點,但然後我如何顯示在我的模板?我的意思是什麼是正確的方式? – Reynier

+0

這是一個更好的主意,異常監聽器,它將檢查類型的異常和攔截響應'JSON' .. –

回答

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 )插入一些角JS可變

<tbody> 
    <tr><td colspan="5">{{ajaxResponse}}</td></tr> 
    <tr ng-repeat="item in MetaDetailGroup"> 
     ... 
相關問題