gpt4 book ai didi

javascript - 在 Symfony2 中验证搜索表单后更新 AngularJS 范围

转载 作者:可可西里 更新时间:2023-11-01 12:21:52 24 4
gpt4 key购买 nike

大家好,

我们必须使用 AngularJS 在 Symfony2 中重写一个软件应用程序,我们将 Symfony2 用于 MVC 目的,将 AngularJS 用于有用的功能。

这是我们的问题,我们首先在我的 Symfony2 View 中使用 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
})
});

{{ url(generatedScope) }} 是一个由 Symfony2 Controller 发送的 Twig 变量,包含以下内容:

 /**
* @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 是我们打开 View 时默认路由的名称 clients/list.html.twig (我们没有使用 Doctrine):

 /**
* @Route("/clients/list/json", name="getClients")
*/
public function getClientsAction()
{
$clients = new Clients($this->get('database_connection'));
$response = new JsonResponse($clients->getClients());
return $response;
}

所以我们的 Controller 发送的 generatedScope 基本上是:127.0.0.0:8000/clients/list/json,这是我们客户端的 json 集合,我们是然后在我们的 View 中以这种方式在表中显示客户:

<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 响应以更新我们的 Angular 范围,我们设法以这种方式检索响应:

 /**
* @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 中验证表单后,我们尝试通过此路由发送 View :

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 Controller :

 /**
* @Route("/tiers", name="index")
*/
public function indexAction()
{
return $this->render('tiers/list.html.twig', [
'generatedScope' => 'searchTiers',
]);
}

generatedScope searchTiers 在我的 Controller 中定义了另一个获取数据的函数:

/**
* @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 将我的输入绑定(bind)到范围

单击我的提交按钮时调用 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 变量,它在我的 S2 Controller 中调用我的 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 的逻辑。

如果你们有任何其他解决方案,请告诉我们!

暂时希望对您有所帮助! :)

迪伦

关于javascript - 在 Symfony2 中验证搜索表单后更新 AngularJS 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35056544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com