【发布时间】:2013-11-27 22:01:02
【问题描述】:
我正在经历 AngularJs 的学习曲线,我发现几乎没有任何示例可以用于现实世界。
我正在尝试清楚地了解如何提交具有最标准组件的表单并将其传递给 PHP 文件..
我的fiddle。
有没有人有任何关于提交简单、无污染的表单的好例子,可以帮助我以及可能许多其他 Angularjs 初学者..
当我说一个干净的形式时,我指的是这样的东西..
<div ng-app="myApp">
<form name="saveTemplateData" action="#" ng-controller="FormCtrl" ng-submit="submitForm()">
First name: <br/><input type="text" ng-model="form.firstname"> <br/><br/>
Email Address: <br/><input type="text" ng-model="form.emailaddress"> <br/><br/>
<textarea rows="3" cols="25" ng-model="form.textareacontent"></textarea>
<br/><br/>
<input type="radio" ng-model="form.gender" value="female" />Female ...
<input type="radio" ng-model="form.gender" value="male" />Male <br/>
<br/><br/>
<input type="checkbox" ng-model="form.member" value="5"/> Already a member
<br/><br/>
<input type="file" ng-model="form.file_profile" id="file_profile"><br/>
<input type="file" ng-model="form.file_avatar" id="file_avatar">
<br/><br/>
<!-- <button ng-click="save()" >Save</button> -->
<input type="submit" ngClick="Submit" >
</form>
</div>
我的 ng-app 代码...
var app = angular.module('myApp', []);
app.controller('FormCtrl', function ($scope, $http) {
var formData = {
firstname: "default",
emailaddress: "default",
textareacontent: "default",
gender: "default",
member: false,
file_profile: "default",
file_avatar: "default"
};
$scope.save = function() {
formData = $scope.form;
};
$scope.submitForm = function() {
console.log("posting data....");
formData = $scope.form;
console.log(formData);
//$http.post('form.php', JSON.stringify(data)).success(function(){/*success callback*/});
};
});
我想我从这里开始的三个问题是......
- 我的 php 文件应该如何与之交互(如何将 json 字符串放入 php 文件中的数组中)?
- 当复选框为真时,我如何提交复选框的值?
- 我发现很多关于使用 jQuery 和 Angular 提交图像的信息,我看到这个提交中已经有一个图像对象,我该如何检索这些数据?图片中应包含哪些注意事项?
我愿意拿出任何清晰简洁的资料,为大家整理一个很好的学习范例……
我的fiddle
【问题讨论】:
-
这是一篇相关的博客文章,我认为它有效地解释了一些基本的 AngularJS 和 HTML 表单交互:thebhwgroup.com/blog/2014/08/…。它没有涵盖您问题的提交部分,但它是实现目标的良好基础。
标签: javascript php forms angularjs