大家好,
我们必须使用 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/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser