提问人:Dylan Gauthier 提问时间:1/28/2016 更新时间:2/12/2016 访问量:483
在 Symfony2 中验证搜索表单后更新 AngularJS 范围
Updating AngularJS scope after validating search form in Symfony2
问:
嗨,各位开发人员,
我们必须使用 AngularJS 在 Symfony2 中重写软件应用程序,我们将 Symfony2 用于 MVC 目的,使用 AngularJS 用于有用的功能。
这是我们的问题,我们首先在我的 Symfony2 视图中使用 AngularJS 在表中显示以下代码的客户端:
var app = angular.module('appName', []);
app.controller('clientsCtrl', function ($scope, $http){
$scope.loading = true;
$http.post('{{ url(generatedScope) }}').then(function(response){
$scope.clients = response.data;
$scope.order = function(predicate){
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse :false;
$scope.predicate = predicate;
}
},function(response){
// TODO: handle the error somehow
})
});
这是 Symfony2 控制器发送的 Twig 变量,具有以下功能:{{ url(generatedScope) }}
/**
* @Route("/clients", name="index")
*/
public function indexAction(Request $request)
{
$form = $this->createForm(SearchClients::class, null);
$form->handleRequest($request);
if($form->isValid()){
//TODO: Get form params and update angularJS scope somehow
}
return $this->render('clients/list.html.twig', [
'generatedScope' => 'getClients',
'form' => $form->createView()
]);
}
getClients
是打开视图时默认路由的名称(我们没有使用 Doctrine):clients/list.html.twig
/**
* @Route("/clients/list/json", name="getClients")
*/
public function getClientsAction()
{
$clients = new Clients($this->get('database_connection'));
$response = new JsonResponse($clients->getClients());
return $response;
}
所以基本上我们的控制器发送的是 : ,它是我们客户端的 JSON 集合,然后我们以这种方式在表中显示客户端:generatedScope
127.0.0.0:8000/clients/list/json
<tr ng-repeat="x in clients | filter : test | orderBy:predicate:reverse">
<td>#{{ x.cli_id }}</td>
<td>{{ x.cli_lastname }}</td>
<td>{{ x.cli_firstname }}</td>
</tr>
我们有一个搜索表单,与显示客户的表格相同的页面,我们收集名字和姓氏,并调用一个操作来检索 json 响应以更新我们的角度范围,我们设法以这种方式检索响应:
/**
* @Route("/tiers/list/json/search", name="searchClients")
*/
public function searchClientsAction(){
$request = new Request($_POST);
$request = $request->query->get('search_clients'); //form name
$clients = new clients($this->get('database_connection'));
$response = new JsonResponse($clients->searchClients($request));
return $response;
}
在验证表单后,我们尝试发送此路由视图:indexAction
if($form->isValid()){
//TODO: Get form params and update angularJS scope somehow
return $this->render('clients/list.html.twig', [
'generatedScope' => 'searchClients',
'form' => $form->createView()
]);
}
但正如你所看到的,它不起作用。
那么,我们如何在验证 Symfony2 表单后更新 angularJS 范围呢?
如果有人遇到过这个问题...很高兴阅读他的意见!
此致敬意
迪伦
答:
@Kodvin是对的,我想出了以下解决方案:
首先,我的 S2 控制器:
/**
* @Route("/tiers", name="index")
*/
public function indexAction()
{
return $this->render('tiers/list.html.twig', [
'generatedScope' => 'searchTiers',
]);
}
generatedScope在我的控制器中定义了另一个获取数据的函数:searchTiers
/**
* @Route("/tiers/list/json/search", name="searchTiers")
*/
public function searchTiersAction(Request $request){
// TODO: A bit of refactoring needed there :-)
$prenom = array('first_name' => $request->request->get('first_name'));
$nom = array('last_name' => $request->request->get('last_name'));
$params['first_name'] = $prenom['first_name'];
$params['last_name'] = $nom['last_name'];
$tiers = new Tiers($this->get('database_connection'));
$response = new JsonResponse($tiers->searchTiers($params));
return $response;
}
我的表单(摆脱了 Symfony 2 表单...但我认为我也可以用 S2 形式做到这一点):
<form method="post" ng-submit="submit()" id="search_tiers">
<input ng-model="last_name" type="text" placeholder="Nom" name="last_name">
<input ng-model="first_name" type="text" placeholder="Prénom" name="first_name">
<input ng-model="submit" type="submit" value="Rechercher" name="submit_form">
</form>
将我的输入绑定到示波器ng-model
在我的 中单击提交按钮时调用 scope 函数submit()
app.controller('tiersCtrl', function ($scope, $http){
$scope.submit = function() {
var req = {
method: 'POST',
url: '{{ url(generatedScope)}}',
headers: {
'Content-Type': 'application/json'
},
data: { last_name: $scope.last_name, first_name: $scope.first_name }
}
$scope.loading = true;
$http(req).then(function(response){
$scope.tiers = response.data;
},function(response){
// TODO: handle the error somehow
});
}
});
{{ url(generatedScope }}
是一个 Twig var,在我的 S2 控制器中调用我的 searchTiersAction() 以返回数据。
最后,显示数据:
<tr ng-repeat="x in tiers">
<td>#{{ x.tie_id }}</td>
<td>{{ x.tie_nom }}</td>
<td>{{ x.tie_prenom }}</td>
<td>{{ x.tie_adresse }}</td>
<td>{{ x.tie_cp }}</td>
<td>{{ x.com_nom }}</td>
<td class="visible-none">{{ x.tpt_nom }}</td>
</tr>
我所缺少的只是我在提交表单时丢失了我的 POST 参数,Ajax 请求完全丢失了......这基本上是 Angular 的逻辑。
如果你们有任何其他解决方案,请告诉我们!
目前,我希望它有所帮助!:)
迪伦
评论
searchClientsAction
Request
Request $request