Reputation: 81
The user adds items -> the model data is pushed to an empty object to create the order. Start with:
$scope.Order = {
details:{},
type1: [],
type2: [],
type3: [],
};
Then:
$scope.addType1 = function ()
$scope.Order.type1.push({
name: $scope.addType1.name,
price: $scope.addType1.price
});
Followed by the remaining types.
I added a WebApi project (so now there are two projects), and I created the same Models in the API.
What I need to do: Post data from Angular to the WebAPI so that an email can be sent containing the order data.
I am familiar with the $Http methods in Angular, but I'm not sure how to check if the data has been transferred to the API, and I'm not sure what my API controller needs. I don't have much experience with server-side code, so any help is appreciated.
Also, let me know if you need more information and I can explain further.
var app = angular.module('myApp', ['ui.bootstrap', 'angularUUID2']);
app.controller('myCtrl', ['$scope', '$http', '$q', 'uuid2', function ($scope, $http, $q, uuid2) {
$scope.order = {
details: {},
type1: [],
type2: []
};
$scope.addOrderDetails = function () {
$scope.addOrderDetails.orderId = uuid2.newguid();
$scope.order.details = $scope.addOrderDetails;
};
$scope.addType1 = function () {
$scope.order.type1.push({
xxx: $scope.addType1.xxx,
yyy: $scope.addType1.yyy,
zzz: $scope.addType1.zzz
});
};
$scope.addType2 = function () {
$scope.order.type2.push({
xxx: $scope.addType2.xxx,
yyy: $scope.addType2.yyy,
zzz: $scope.addType2.zzz
});
};
var deferred = $q.defer();
var url = 'http://localhost:xxxxxx/api/order';
var data = $scope.order;
$scope.submitOrder = function () {
$http.post(url, data).success(function (response) {
console.log(response);
deferred.resolve(response)
}).error(function (error) {
console.log(error);
deferred.reject(error)
});
return deferred.promise;
};
}]);
Here is the OrderController Post method:
[HttpPost]
public Order Post([FromBody]Order model)
{
return model;
}
Upvotes: 0
Views: 1165
Reputation: 4232
uksz is right!
just wanted to show how you would post your data
var url = your url to post to here e.g 'www.somedomain.com/orders';
var data = put your data object here e.g. $scope.order;
$http.post(url,data).success(function(response) {
console.log(response);
}).error(function(error) {
console.log(error);
});
You would usually want to implement a promise in this situation, which you can do like so (just make sure you include the $q library in your dependencies
var deferred = $q.defer()
var url = your url to post to here e.g 'www.somedomain.com/orders';
var data = put your data object here e.g. $scope.order;
$http.post(url,data).success(function(response) {
console.log(response);
deferred.resolve(response)
}).error(function(error) {
console.log(error);
deferred.reject(error)
});
return deferred.promise
Upvotes: 0
Reputation: 18719
There you have documentation for $http service, which allows to POST and GET.
https://docs.angularjs.org/api/ng/service/$http
So:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Basically, if the response is a success response (server responds positively), the callback gets executed. The same for failure.
Upvotes: 2