javascript – 使用隐藏值发送POST在AngularJs中不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用隐藏值发送POST在AngularJs中不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3677字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 使用隐藏值发送POST在AngularJs中不起作用](/upload/InfoBanner/zyjiaocheng/785/1439551a570446af8ac44002b7696633.jpg)
在我的网络应用程序中,页面上有许多表单.我想用AngularJS提交特定表格.
在每种形式中,它都需要具有隐藏价值的唯一ID才能提交.但是看到的值=“UNIQUE_ID”在AngularJS的隐藏输入框中不起作用.
我的HTML
<div ng-app>
<div ng-controller="SearchCtrl">
<form class="well form-search">
<input type="text" ng-model="keywords" name="qaq_id" value="UNIQUE_ID">
<pre ng-model="result">
{{result}}
</pre>
<form>
</div>
</div>
这是js脚本
function SearchCtrl($scope, $http) {
$scope.url = 'qa/vote_up'; // The url of our search
// The function that will be executed on button click (ng-click="search()")
$scope.search = function() {
// Create the http post request
// the data holds the keywords
// The request is a JSON request.
$http.post($scope.url, { "data" : $scope.keywords}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data; // Show result from server in our <pre></pre> element
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}
解决方法:
可能是您的代码无法工作的唯一原因是$scope.keywords是一个简单的变量(带有文本值)而不是Object,这是必需的 – 请参阅http://docs.angularjs.org/api/ng.$http#Usage
由于angularJS使用其自身范围内的变量 – 其模型,表单只是一种与这些模型交互的方式,可以通过您想要的任何方法发送.
你可以有一个隐藏的字段,是的,但在angularJS中甚至没有必要.您只需要在控制器本身中定义信息 – 为每个实例随机生成,或从其他来源接收.或者您可以在加载控制器时自行定义它.
所以(并且只是为了清晰起见)如果你在formCtrl中定义一个formData变量:
你的HTML:
<div ng-app>
<div ng-controller="SearchCtrl">
<form class="well form-search">
<input type="text" ng-model="formData.title">
<input type="textarea" ng-model="formData.body">
<button ng-click="sendData()">Send!</button>
</form>
<pre ng-model="result">
{{result}}
</pre>
</div>
</div>
而你的控制器:
function SearchCtrl($scope, $http) {
$scope.url = 'qa/vote_up'; // The url of our search
// there is a formData object for each instance of
// SearchCtrl, with an id defined randomly
// Code from https://stackoverflow.com/a/1349426/1794563
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
$scope.formData = {
title: "",
text: ""
};
$scope.formData.id = makeid();
// The function that will be executed on button click (ng-click="sendData()")
$scope.sendData = function() {
// Create the http post request
// the data holds the keywords
// The request is a JSON request.
$http.post($scope.url, { "data" : $scope.formData}).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.result = data; // Show result from server in our <pre></pre> element
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}
另外:如果你想在html本身设置唯一的id,你可以添加一个输入类型=“hidden”并将它的ng-model属性设置为formData.id,无论你设置它的值,模型都会有它绑定.使用隐藏输入将不起作用,因为value属性不会更新通过ng-model分配的angularJS模型.请改用ng-init来设置值:
包含2种形式的HTML:
<div ng-controller="SearchCtrl" ng-init="formData.id='FORM1'">
<form class="well form-search">
<input type="text" ng-model="formData.title">
<input type="textarea" ng-model="formData.body">
<button ng-click="sendData()">Send!</button>
</form>
</div>
<div ng-controller="SearchCtrl" ng-init="formData.id='FORM2'">
<form class="well form-search">
<input type="text" ng-model="formData.title">
<input type="textarea" ng-model="formData.body">
<button ng-click="sendData()">Send!</button>
</form>
</div>
您可以添加隐藏字段,但它不会完成任何操作 – ng-init属性可以完成您需要的所有操作.
内容总结
以上是互联网集市为您收集整理的javascript – 使用隐藏值发送POST在AngularJs中不起作用全部内容,希望文章能够帮你解决javascript – 使用隐藏值发送POST在AngularJs中不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。