使用fos-rest渲染模板然后关闭弹出窗口

时间:2016-05-20 08:22:22

标签: angularjs symfony fosrestbundle

我在页面上有一个按钮,当单击它时,它会打开一个包含表单(类型)的弹出窗口。我设法渲染表格。提交表单时,已完成对db的添加,但我将在新窗口中重定向到表单模板/路由。我想做的是关闭弹出窗口,不重定向到另一个页面。

从角度

开始
function FeedbackController (modalService) {
    var vm = this;

    vm.open = open;

    function open () {
        modalService.openModal('add_feedback');
    }
}

路线:

add_feedback:
    path: /feedback
    defaults:
        _controller: MainBundle:Api/Feedback:addFeedback
        template:    MainBundle:Modals:feedback.html.twig
    options:
        expose: true

行动:

 /**
 * @FosRest\View()
 */
 public function addFeedbackAction(Request $request)
 {
   $view = View::create();

   $feedback = new Feedback();
   $feedbackService = $this->get('main.feedback.service');
   $form = $this->createForm(new FeedbackType(), null, ['action' => 'feedback']);
   $form->handleRequest($request);

   if ($form->isValid()) {
       $formData = $form->getData();
       $feedbackService->create($formData, $feedback);

       return null;
   }
   $view
     ->setData($form)
     ->setTemplateData($form)
     ->setTemplate('MainBundle:Modals:feedback.html.twig');

   return $view;
    //        return $this->render('MainBundle:Modals:feedback.html.twig', array(
    //            'form' => $form->createView(),
    //        ));
        }

1 个答案:

答案 0 :(得分:0)

因此,您应该使用ajax查询发布表单,等待响应状态并关闭弹出窗口或显示消息,如果出现错误。

以下是一个例子:

angular.module('myApp', [])
  .controller('myCtrl', function($scope, $http) {
    $scope.hello = {
      name: "Boaz"
    };
    $scope.newName = "";
    $scope.sendPost = function() {
      var data = $.param({
        json: JSON.stringify({
          name: $scope.newName
        })
      });
      $http.post("/echo/json/", data).success(function(data, status) {
        $scope.hello = data;
      })
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='myApp'>

  <div ng-controller="myCtrl">
    {{hello.name}}

    <form ng-submit="sendPost()">
      <input ng-model="newName" />
      <button type="submit">Send</button>
    </form>

  </div>

</body>