gpt4 book ai didi

angularjs - 注入(inject) $http 后,代码未在 angular.js 中显示表格

转载 作者:行者123 更新时间:2023-12-02 03:16:09 24 4
gpt4 key购买 nike

我是 anular.js 的初学者。我正在尝试编写一个代码,在单击按钮时添加和删除表中的行,一旦完成广告用户单击 createdelivery 按钮,我想将数据作为 JSON 对象发送到服务器。

但是当我如下注入(inject) $http 时,

var app = angular.module("createDelivery", []);
app.controller("MyController", ['$scope', 'http', function($scope, $http)

它也不会创建表。

请帮我解决这个问题。

<html>
<head>

<title>Add Rows</title>


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script>
var app = angular.module("createDelivery", []);
app.controller("MyController", ['$scope', 'http', function($scope, $http) {
$scope.deliveryDetails = [
{
'fname':'Muhammed',
'lname':'Shanid',
'email':'shanid@shanid.com'
},

{
'fname':'Roy',
'lname':'Mathew',
'email':'roy@roy.com'
}];

$scope.addNew = function(cdDetails){
$scope.deliveryDetails.push({
'fname': "",
'lname': "",
'email': "",
});
};

$scope.remove = function(){
var newDataList=[];
$scope.selectedAll = false;
angular.forEach($scope.deliveryDetails, function(selected){
if(!selected.selected){
newDataList.push(selected);
}
});
$scope.deliveryDetails = newDataList;
};

$scope.checkAll = function () {
if (!$scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.deliveryDetails, function(cdDetails) {
cdDetails.selected = $scope.selectedAll;
});
};
$scope.getData = function(cdDetails)
{
alert("Check");
var jsonString;
jsonString = angular.toJson(cdDetails);
//JSON.stringify(jsonString);
$http
({
url: "/scheduler/createDelivery",
dataType: 'json',
method: 'POST',
data: jsonString

}).success(function(response) {
alert("success : : "+jsonString);
$scope.value = response;
}).error(function(error) {
alert("error::"+error);
});

//window.location.reload();
//document.write("\nString value "+jsonString);
};

}]);
</script>
</head>

<body ng-app="createDelivery" ng-controller="MyController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form ng-submit="addNew()">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /></th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cdDetails in deliveryDetails">
<td>
<input type="checkbox" ng-model="cdDetails.selected"/></td>
<td>
<input type="text" class="form-control" ng-model="cdDetails.fname" required/></td>
<td>
<input type="text" class="form-control" ng-model="cdDetails.lname" required/></td>
<td>
<input type="email" class="form-control" ng-model="cdDetails.email" required/></td>
</tr>
</tbody>
</table>

<div class="form-group">
<input ng-hide="!deliveryDetails.length" type="button" class="btn btn-danger pull-right" ng-click="remove()" value="Remove">
<input type="submit" class="btn btn-primary addnew pull-right" value="Add New">
<button type="button" name = "createDelivery"class="col-sm-2 btn btn-primary" style="width:25%" ng-click="getData(cdDetails);">Create Delivery</button>

</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

最佳答案

 app.controller("MyController", ['$scope', 'http'

上面的代码有一个错误。改变它并尝试。你必须注入(inject) $http

关于angularjs - 注入(inject) $http 后,代码未在 angular.js 中显示表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36965454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com