Angularjs 将文本框值以角度传递给服务
我有两个页面,一个是创建数据,当我单击“保存”按钮时,文本框值将发送到后端方法。Angularjs 将文本框值以角度传递给服务,angularjs,Angularjs,我有两个页面,一个是创建数据,当我单击“保存”按钮时,文本框值将发送到后端方法。在另一个页面中,我将在表中显示输入的数据。因此,我在这里使用两种控制器方法。 我不知道如何使用angular将文本框值发送到后端。我只是使用jquery实现了这一点,但我不知道angular,这就是为什么我在努力。 这是我保存文本框详细信息的第一页: <div class="row "> <label>Name<span id="required">*
在另一个页面中,我将在表中显示输入的数据。因此,我在这里使用两种控制器方法。
我不知道如何使用angular将文本框值发送到后端。
我只是使用jquery实现了这一点,但我不知道angular,这就是为什么我在努力。
这是我保存文本框详细信息的第一页:
<div class="row ">
<label>Name<span id="required">*</span></label>
<input type="text" ng-model="item.name" class = "col-lg-5 col-md-5 col-sm-5 col-xs-10"/>
</div>
<div class="row ">
<label class="col-lg-2 col-md-2 col-sm-3 col-xs-12 ">Description</label>
<textarea ng-model="item.description"></textarea>
</div>
<div class="row marTop">
<span class="pull-right">
<button class="btn save btn-primary" ng-click="addItem(item)"><i class="fa fa-floppy-o"></i>Save</button>
<button class="btn cancel btn-default" onclick="window.location.href = '/Admin/RoleList'"><i class="fa fa-ban"></i>Cancel</button>
</span>
</div>
这是我获取表格数据的第二页(保存的name
应显示在此表格中)
美国号
名称
行动
{{x.Id}
{{x.name}
第二个表的脚本
<script>
var app=angular
.module("intranet_App", [])
.controller('myCtrl', function ($scope, $http) {
$scope.values = {
};
$http.post("/Admin/getRolesList")
.then(function (response) {
$scope.roleList = response.data;
});
})
</script>
var app=角度
.模块(“intranet_应用程序”,[])
.controller('myCtrl',函数($scope,$http){
$scope.values={
};
$http.post(“/Admin/getRolesList”)
.then(函数(响应){
$scope.roleList=response.data;
});
})
执行此操作的方法之一是:
angular.module("intranet_App", [])
.controller('myCtrl', function ($scope, $http, myFactory) {
...
function sendDataToBackend() {
var requestHeaders = {
"Content-Type": 'application/json'
}
var httpRequest = {
method: 'POST',
url: 'your url',
headers: requestHeaders
};
httpRequest.data = $scope.items;
$http(httpRequest).then(function (response) {
//Handle the response from the server
})
}
}
终于得到了答案:
<script>
var app=angular
.module("intranet_App", [])
.controller('myCtrl', ["$scope","$http", function ($scope, $http ,myFactory) {
$scope.items = [];
$scope.addItem = function (item) {
$scope.items.push(item);
//$scope.oRoles = $scope.item
$scope.json = angular.toJson($scope.item);
console.log($scope.json)
}
$scope.myFunc = function (item) {debugger
$scope.addItem(item);
var requestHeaders = {
"Content-Type": 'application/json'
}
var httpRequest = {
method: 'POST',
url: '/Admin/RolesInsert',
headers: requestHeaders
};
httpRequest.data = $scope.json;
$http(httpRequest).then(function (response) {
alert(response)
//$window.location.href = '/Admin/RoleList';
})
}
}])
</script>
var app=角度
.模块(“intranet_应用程序”,[])
.controller('myCtrl',[“$scope”,“$http”,函数($scope,$http,myFactory){
$scope.items=[];
$scope.addItem=函数(项){
$scope.items.push(项目);
//$scope.oRoles=$scope.item
$scope.json=angular.toJson($scope.item);
log($scope.json)
}
$scope.myFunc=函数(项){debugger
$scope.addItem(项目);
var requestHeaders={
“内容类型”:“应用程序/json”
}
var httpRequest={
方法:“POST”,
url:“/Admin/RolesInsert”,
标题:请求标题
};
httpRequest.data=$scope.json;
$http(httpRequest)。然后(函数(响应){
警报(响应)
//$window.location.href='/Admin/RoleList';
})
}
}])
您想在post
方法中将$scope.items
数据发送到后端,对吗?是。在jquery中,我这样发送var data={name:name,description:description}代码>这样我只需要通过。您可以这样做:$http.post(“/Admin/RolesInsert”,$scope.items)
您可以在这里引用,在console.log中写入“$scope.items”,而不是“item”。item变量未定义,而$scope.items具有值。建议不要使用post方法从后端获取数据,而是使用get方法。item
和$scope.item
是两个不同的变量。请改为执行此操作:console.log($scope.item)
您得到了它。但是只有对象类似于object{}
我的值不在对象内部。在它正上方的语句中,您是否将{}
分配给$scope.item
?因此它将打印{}
。如果您想查看列表,是否应该console.log($scope.items)
?我们为什么不直接在httpRequest
中给出“内容类型”:“application/json”
,这就是$http
api的设计方法:。这是有意义的,因为头是HTTP请求的一部分。
angular.module("intranet_App", [])
.controller('myCtrl', function ($scope, $http, myFactory) {
...
function sendDataToBackend() {
var requestHeaders = {
"Content-Type": 'application/json'
}
var httpRequest = {
method: 'POST',
url: 'your url',
headers: requestHeaders
};
httpRequest.data = $scope.items;
$http(httpRequest).then(function (response) {
//Handle the response from the server
})
}
}
<script>
var app=angular
.module("intranet_App", [])
.controller('myCtrl', ["$scope","$http", function ($scope, $http ,myFactory) {
$scope.items = [];
$scope.addItem = function (item) {
$scope.items.push(item);
//$scope.oRoles = $scope.item
$scope.json = angular.toJson($scope.item);
console.log($scope.json)
}
$scope.myFunc = function (item) {debugger
$scope.addItem(item);
var requestHeaders = {
"Content-Type": 'application/json'
}
var httpRequest = {
method: 'POST',
url: '/Admin/RolesInsert',
headers: requestHeaders
};
httpRequest.data = $scope.json;
$http(httpRequest).then(function (response) {
alert(response)
//$window.location.href = '/Admin/RoleList';
})
}
}])
</script>