简体   繁体   中英

AngularJS: post data to server

I want to add data to my database using AngularJS. First I have to choose a category from a list of categories which I get from my server. After that a user can add a product to the database. I tried accomplishing this using:

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>  

I am not even using the ng-models to use the data of my html. It's still hard coded because it doesn't even work then. I am getting en error:

TypeError: Cannot read property 'post' of undefined

What am i doing wrong with my $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){
        });
        }
    });
});

Remove $http parameter from $scope.product function.

Also, look at https://docs.angularjs.org/tutorial/step_05 about angular dependency issue during minification and how to solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM