详细分析使用AngularJS编程中提交表单的方式(3)

这是Angular的核心思想之一,也是功能最强大的内容之一。在Angular文档中,我们看到:“在Angular网页应用中的数据绑定,是模型和视图层之间的数据自动同步。”这意味着,我们需要在表单中抓取数据,使用$('input[name=name]').val()并不是必需的。

我们在Angular中将数据和变量绑定在一起,无论是javascript也好,view也罢,只要有改变,两者皆变。

为了演示数据绑定,我们需要获取表单的input来自动填充变量formData。让我们回到应用于页面的Angular控制器中。我们在过一下$scope和$http。

$scope:控制器和视图层之间的粘合剂。基本上,变量使用$scope从我们的控制器和视图层之间传递和往来。具体详细的定义,请参见文档。

$http:Angular服务来帮助我们处理POST请求。更多信息,请参见文档。

使用数据绑定获取变量

好了,闲话少说。我们将这些讨论应用到表单中去。方法比上面讨论的要简单。我们想Angular控制器和视图中分别添加一行。
 

<!-- index.html --> ... <!-- PROCESS FORM WITH AJAX (NEW) --> <script> // define angular module/app var formApp = angular.module('formApp', []); // create angular controller and pass in $scope and $http function formController($scope, $http) { // create a blank object to hold our form information // $scope will allow this to pass between controller and view $scope.formData = {}; } ...

现在,我们已经建立了一个formData对象。让我们用表单数据来填充它。在显示调用每个输入和获得val()之前,我们用ng-model绑定一个特殊的输入到变量。
 

<!-- index.html --> ... <!-- FORM --> <form> <!-- NAME --> <div> <label>Name</label> <input type="text" placeholder="Bruce Wayne" ng-model="formData.name"> <span></span> </div> <!-- SUPERHERO NAME --> <div> <label>Superhero Alias</label> <input type="text" placeholder="Caped Crusader" ng-model="formData.superheroAlias"> <span></span> </div> <!-- SUBMIT BUTTON --> <button type="submit"> <span></span> Submit! </button> </form> <!-- SHOW DATA FROM INPUTS AS THEY ARE BEING TYPED --> <pre> {{ formData }} </pre> ...

现在,既然Angular已经将每个输入绑到了formData。 当你输入每个输入框,你可以看到formData对象被填充了!有没有很酷!

你不必在view中使用$scope。一切被认为是嵌入到$scope中的。
 
处理表单

在我们的旧表单中,我们使用jQuery提交表单,像这样$('form').submit()。现在我们使用Angular称作ng-submit的特性。要想完成这个,我们需要添加一个控制器函数来处理表单,然后告诉我们form使用这个控制器函数:
 

<!-- index.html --> ... <!-- PROCESS FORM WITH AJAX (NEW) --> <script> // define angular module/app var formApp = angular.module('formApp', []); // create angular controller and pass in $scope and $http function formController($scope, $http) { // create a blank object to hold our form information // $scope will allow this to pass between controller and view $scope.formData = {}; // process the form $scope.processForm = function() { }; } ... <!-- FORM --> <form ng-submit="processForm()"> ...

现在我们的form知道提交时使用控制器函数了。既然已经到位了,然我们用$http来处理表单吧。

处理表单的语法看起来跟原始方式很像。好处是我们不需要手动抓取表单数据,或者注入,隐藏,添加类显示错误或成功信息。
 

<!-- index.html --> ... // process the form $scope.processForm = function() { $http({ method : 'POST', url : 'process.php', data : $.param($scope.formData), // pass in data as strings headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) }) .success(function(data) { console.log(data); if (!data.success) { // if not successful, bind errors to error variables $scope.errorName = data.errors.name; $scope.errorSuperhero = data.errors.superheroAlias; } else { // if successful, bind success message to message $scope.message = data.message; } }); }; ...

这就是我们的表单!没有添加或移除类。我们需要每次提交表单时都清楚错误。我们只需绑定变量和需要用到的视图。这非常棒,因为处理器用来处理数据,而视图用来显示数据.


jQuery POST vs Angular POST

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgdfxp.html