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


Updating AngularJS scope after validating search form in Symfony2

嗨,各位开发人员,

我们必须用AngularJS在Symfony2

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

这是我们的问题,我们首先在我的 Symfony2 视图中使用以下代码将客户端显示在一个表中:

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 控制器发送的 Twig var,如下所示:

 /**
 * @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 是打开视图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;
}

所以基本上我们的控制器发送的generatedScope是:127.0.0.0:8000/clients/list/json,这是我们客户端的 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',
    ]);
}

生成的范围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 将我的输入绑定到范围

在单击我的提交按钮时调用 submit() scope 函数

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 控制器中调用我的搜索 TiersAction() 来返回数据。

最后,显示数据:

<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的逻辑。

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

目前,我希望它有所帮助! :)

迪伦