gpt4 book ai didi

javascript - AngularJS:将数据发布到服务器

转载 作者:行者123 更新时间:2023-12-03 10:10:03 26 4
gpt4 key购买 nike

我想使用 AngularJS 将数据添加到我的数据库中。首先,我必须从从服务器获得的类别列表中选择一个类别。之后,用户可以将产品添加到数据库中。我尝试使用以下方法来完成此操作:

AngularJS

categories = angular.module('categories', []);
categories.controller("category",function($scope, $http){
var serviceBase = 'api/';
$http.get(serviceBase + 'categories').then(function (results) {
$scope.categories = results.data;
for(var i = 0; i < $scope.categories.length; i++){
var categories = $scope.categories[i];
}
$scope.product = function($scope, $http, $catID){
$http.post(serviceBase + 'productadd/3/1/Icetealemon/5').then(function(results){
});
}
});
});

HTML

<table id="app2" ng-app="categories" ng-cloak="" class="table table-hover">
<tr >
<th colspan="5">Add product</th>
</tr>
<tr ng-form name="addproductForm" novalidate ng-controller="category">
<td colspan="1">
<select class="form-control m-b-10">
<option ng-repeat= "c in categories">{{c.categoryName}}</option>
</select>
</td>
<td colspan="1">
<select class="form-control m-b-10">
<option>Antwerpen</option>
<option>Leuven</option>
</select>
</td>
<td colspan="1">
<input type="text" class="form-control" placeholder="Name" ng-model="catID"></input>
</td>
<td colspan="1">
<input type="text" class="form-control" placeholder="Price" width="10%"></input>
</td>
<td colspan="1">
<input type="submit" class="btn btn-success" ng-click="product()"></input>
</td>
</tr>
</table>

我什至没有使用 ng-models 来使用我的 html 数据。它仍然是硬编码的,因为那时它甚至不起作用。我收到错误:

TypeError: Cannot read property 'post' of undefined

我的 $http.post 做错了什么?

最佳答案

categories = angular.module('categories', []);
categories.controller("category",function($scope, $http){
var serviceBase = 'api/';
$http.get(serviceBase + 'categories').then(function (results) {
$scope.categories = results.data;
for(var i = 0; i < $scope.categories.length; i++){
var categories = $scope.categories[i];
}
$scope.product = function($scope, $http, $catID){


// NOTE: This '$http' is $scope.product function parameter
// variable `$http` not angular '$http'


$http.post(serviceBase + 'productadd/3/1/Icetealemon/5').then(function(results){
});
}
});
});

$scope.product 函数中删除 $http 参数。

另外,请查看https://docs.angularjs.org/tutorial/step_05关于缩小过程中的 Angular 依赖性问题以及如何解决它。

关于javascript - AngularJS:将数据发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164415/

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