notes-create-controller.js 735 B

123456789101112131415161718192021222324252627282930313233
  1. angular.module("noteWrangler")
  2. .controller("NoteCreateController", function ($http) {
  3. const controller = this;
  4. this.saveNote = function (note) {
  5. controller.errors = null;
  6. $http({
  7. method: "POST",
  8. url: "/notes.json",
  9. data: note,
  10. }).catch(function (note) {
  11. controller.errors = note.data.error;
  12. })
  13. .then(responseHandler, errorHandler);
  14. };
  15. const errorHandler = function (err) {
  16. console.log("Error getting /notes", err);
  17. };
  18. const responseHandler = function (data) {
  19. controller.notes = data.data;
  20. };
  21. });
  22. /* Somewhere in templates, to show the error:
  23. <p ng-if="createController.errors">
  24. {{ createController.errors }}
  25. </p>
  26. */