I'm just getting started with angular and coding in general so bear with me...
I'm currently trying to write a basic phonebook-like CRUD app. So far, I have no trouble adding and viewing contacts, but I'm stuck on how to update my model on MongoLab via $http.put and MongoLab's API
Any help or pointers in the right direction would be greatly appreciated!
Here's my UpdateCtrl function and related form attached (placed a generic URL here for demonstration purpose):
function UpdateCtrl($scope, $http, $routeParams, $location){
$scope.contactId = $routeParams.contactId;
var url = 'https://api.mongolab.com/api/databases/<d>/collections/<c>/<id>;
$http.get(url).
success(function(mongoDataById) {
$scope.contact = mongoDataById;
});
$scope.updateContact = function() {
$http.put(url, $scope.contact);
};
}
<form ng-submit="updateContact()">
<label for="fullname">Name</label>
<input type="text" id="fullname" ng-model="contact.fullname" ><br/>
<label for="email">Email</label>
<input type="text" id="email" ng-model="contact.email"><br/>
<label for="phone">Phone</label>
<input type="text" id="phone" ng-model="contact.phone"><br/>
<button type="submit">Update</button>
</form>
@Stewie and @Arun P Johny, there do not seem to be any errors in the console when I execute updateContact(). I suspect it is sending a PUT request with the same data resulting from the initial GET request, even though changes have been made in the form in the current $scope. (And yes, I have a specific contactId in my Url - tested in browser, and it pulls the data for specific contact I've selected)
@pkozlowski.opensource - yeah I actually added that library when I started on this project, but when I was able to get away with fairly simple $http POST and GET functions, I felt like it wasn't necessary. Might give it a try now if I can't figure out using the basic $http.put.
You might need an API key in order to access Mongo Lab's RESTful API such as:
GET /databases/{database}/collections/{collection}
Example listing all documents in a given collection:
https://api.mongolab.com/api/1/databases/my-db/collections/my-coll?apiKey=myAPIKey
Optional parameters [q=][&c=true][&f=][&fo=true][&s=][&sk=][&l=]
More info at: http://docs.mongolab.com/restapi/#authentication
It would be nice if you could tell us if Get commant works correctly