Tell me how you can process the received response from the server and, depending on the response, redirect the user to the desired page. I can not figure out how to do it. In general, the essence is as follows: There is a html form.

<form name="codeForm" novalidate> <input name="code" type="text" placeholder="Enter your code" ng-model="confirm.code" required> <a href="/nextpage"><button ng-click="send()" ng-disabled="codeForm.$invalid">Next</button></a> </form> 

When you click on a button, the data that was filled in the input is sent to the server:

 angular .module('app') .factory('myFactory', myFactory); function myFactory($http, $q) { return { confirmPhoneNumber: function(){ var deferred = $q.defer(); $http({ url: "http://example:8080/mimimi/", method: "POST", data: data, }).success(function(data, status, headers, config){ deferred.resolve(data); console.log(data) }).error(function(data, status, headers, config){ deferred.reject(status); console.log(status); }); return deferred.promise; } } } 

After we get the answer, whether the user entered the data correctly or not. If successful, returns: success: true, otherwise: success: false.

QUESTION: If the answer is false, how to leave the user on the same page? and in the case of c true, allow the transition to the next one (the link is bolted to the button). Please tell me how you can do this, or share links to relevant articles!

PS: I use ui-router routing.

  • What prevents to do check in send () and in the same place to receive value href? - fedornabilkin
  • firstly: $ http - already returns promise, there is no need to do it manually through $q.defer() , and secondly - to use the button block element inside inline - the links are somehow not very, and it is not clear why. Click handler can be hung on the link - Grundy
  • Well, and it’s still worth adding the send function - Grundy
  • Why do you even use $q.defer() - if the $http() function already returns the promise to you? - Pavel Mayorov
  • @PavelMayorov That is, you can do without $ q.defer (), which means I misunderstand why it is needed. It was just that earlier I could not extract the data and work with them without using $ q.defer (). If it does not complicate, can you explain briefly when to use it? - Artem

1 answer 1

You do not check there. You need to check the data in the resolve ui-router. If true returns, then go to the page.