简体   繁体   English

如何使用AngularJS表单将POST数据发送到Play Framework Controller

[英]How to send POST data with an AngularJS form to Play Framework Controller

I have on my web application (Play Framework 2.2.1 & Scala) a list of customers who are displayed with ajax and AngularJS. 我的Web应用程序(Play Framework 2.2.1和Scala)上有一个与ajax和AngularJS一起显示的客户列表。 It works fine, and now I want to add a new customer dynamically in my list, with a JS pop up I've made (with jQuery UI). 它工作正常,现在我想在列表中动态添加一个新客户,并弹出一个JS弹出窗口(使用jQuery UI)。

I search first how to display on browser data sended by the HTML/AngularJS form, but when I click on my submit button, nothing happen... And I don't know why, other AngularJS actions works. 我首先搜索如何在HTML / AngularJS表单发送的数据上显示在浏览器上,但是当我单击“提交”按钮时,什么也没有发生……而且我不知道为什么,其他AngularJS动作仍然有效。

Here is my code : 这是我的代码:

  • JavaScript 的JavaScript

     function AddCustomerController($scope) { $scope.add = function() { $scope.customers.push({id: $scope.email, phone: $scope.phone, isActivated: $scope.activation, name: $scope.name, roleType: $scope.roleType}); } } 
  • HTML 的HTML

     <div id="dialogAddCustomer" title="title" ng-controller="AddCustomerController"> <label id="dialNewCustomerName">Add Customer</label> <label class="lblPopUp">Email</label> <input type="text" id="dialNewCustomerEmail" class="form-control" ng-model="email" /> <label class="lblPopUp">Phone Number</label> <input type="text" id="dialNewCustomerPhone" class="form-control" ng-model="phone" /> <label class="lblPopUp">Customer Activated ?</label> <br /> <input type="radio" name="activation" id="dialNewCustomerYes" value="1" ng-model="activation" /> Yes <input type="radio" name="activation" id="dialNewCustomerNo" value="2" ng-model="activation" /> No <label class="lblPopUp">Name</label> <input type="text" id="dialNewCustomerName" class="form-control" ng-model="name" /> <label class="lblPopUp">Role Type</label> <br /> <select class="form-control" ng-controller="RoleTypesController"> <option ng-repeat="roleType in roleTypes" value="{{roleType.id}}" ng-model="roleType">{{roleType.name}}</option> </select> <br /> <button class="btn btn-default" type="submit" ng-click="add()">Validate</button> </div> 

Second, I don't know how to push data to my controller (I've searched already on the web) correctly to insert new customer in database. 其次,我不知道如何正确地将数据推送到我的控制器(我已经在网上搜索过)以在数据库中插入新客户。

Any idea, or tips ? 有什么想法或提示吗?

Maybe you should add AddCustomerController to the HTML, or move the add() function to the CustomerController . 也许您应该将AddCustomerController到HTML,或将add()函数移动到CustomerController

You should also just put a customer object into scope, and add the individual fields to it, and then access it via customer.email , customer.phone and so on. 您还应该将customer对象放入范围,并向其中添加各个字段,然后通过customer.emailcustomer.phone等访问它。 That will help prevent scoping issues and also make the controller code a lot simpler. 这将有助于防止范围问题,并使控制器代码更加简单。

Also, this has nothing to do with Play, as you don't send any data to the server (check $http in Angular's documentation on how to do this). 另外,这与Play无关,因为您不会将任何数据发送到服务器(有关如何执行此操作,请查看Angular文档中的$ http)。

Nothing is ever sent to the server because the code never instructs AngularJS to send anything. 没有任何内容发送到服务器,因为该代码从不指示AngularJS发送任何内容。

Your add function modifies local scope only, and doesn't invoke $http.post() . 您的add函数仅修改本地作用域,并且不会调用$http.post()

You may be thinking too much in html terms and not enough in AngularJS terms. 您可能在html术语中考虑过多,而在AngularJS术语中考虑不够。 With angular you don't need to have a form; 使用angular,您不需要表单; it can be handy - particularly for validation - but it's not required. 它可能很方便-特别是对于验证-但这不是必需的。 The model is what is key, with or without a form. 模型是关键,有或没有表格。

In general, I suggest having your input s modify parts of an object, so instead of: 通常,我建议让您的input修改对象的某些部分,而不是:

<input type="text" ng-model="email" />
<input type="text" ng-model="phone" />
<button type="submit" ng-click="add()">Add</button>

do instead: 改为:

<input type="text" ng-model="customer.email" />
<input type="text" ng-model="customer.phone" />
<button type="submit" ng-click="add(customer)">Add</button>

Controller then something like: 控制器然后像:

function AddCustomerController($scope, $http) {
    $scope.customer = {};
    $scope.add = function(newCustomer) {
        // this assumes newCustomer has been validated
        $http.post('/api/customers', newCustomer); // TODO: handle response
    }
}

As a side note, the more you get into angular, the less you'll need jquery and jquery ui - I have a rule that a page is either jquery or angular, but never both. 附带一提,您对角度的了解越多,对jquery和jquery ui的需求就越少-我有一个规则,即页面要么是jquery要么是角度页面,但决不要同时使用。 YMMV. YMMV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM