我有一个我不想发送的输入字段。即使我删除了输入字段上的名称,它仍然可能由于角度魔法而被发送。
为了防止这种情况,我想如果我可以从邮寄请求中删除此项,那么它就是解决方案。
<input type='radio' ng-model='birthday' ng-value='true'>
表单提交POST时,尽管输入没有name属性,但仍有一个名为birthday的字段。那么我该如何防止它出现呢。
表格是html模板,控制器在ng-submit上调用
答案 0 :(得分:0)
我认为您可能正在寻找disabled
属性:
<input type='radio' ng-model='birthday' ng-value='true' disabled="true">
<强>被修改强>
以下是如何使用disabled属性在提交时不使用表单发送不需要的信息的示例:
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var TestApp = angular.module("TestApp", []);
TestApp.controller('TestCtrl', function($scope) {
$scope.needsBirthdayDisabled = false;
$scope.needsBirthday = false;
$scope.sendForm = function(form) {
$scope.needsBirthdayDisabled = true; //if you comment this line, the "yep"'s input value will be sent with the form
form.$submitted = true;
};
});
</script>
<div ng-app="TestApp" ng-controller="TestCtrl">
<!-- the method is set to GET for test purpose (so we can easily see the sent values on the URL) -->
<form action="" method="GET" name="myForm" ng-submit="sendForm(this)">
<div>
<label for="name">Name</label>
<input type="text" name="name" ng-model="name"/>
</div>
<div ng-show="needsBirthday">
<label for="birthday">Birthday</label>
<input type="text" name="birthday" ng-model="birthday"/>
</div>
<div>
<label>Needs Birthday</label>
Yep <input type='radio' name="yep" ng-model='needsBirthday' ng-value='true' ng-disabled="needsBirthdayDisabled">
</div>
<input type="submit" value="Go!"/>
</form>
</div>
</body>
</html>