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

标签 javascript angularjs

我想使用 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/

相关文章:

javascript - Protractor 是否兼容 IE 9 浏览器?

javascript - 如何限制一个元素在一分钟内被点击的次数?

javascript - 如何从 yadcf 列(搜索)过滤器中排除隐藏元素?

javascript - 在 chrome 新版本上使用 window.open 显示 base64 pdf 数据

angularjs - 将模块 "angular-hotkeys"添加到我的 mean.js 应用程序

angularjs - 基于 Controller bool 变量显示/隐藏元素AngularJS

javascript - 当我在跨度 Javascript 上单击它们时,元素不会消失

javascript - 在 javascript 中创建一个 Json 对象

javascript - 单击 1 次后灰色按钮 - HTML/CSS/JS

javascript - 如何控制 Angular 转换以避免看到内容加载