Form feed in ui-router corner

I am using angular -ui-router. When the form is submitted, I want to activate ui-routing, which is defined using ui-sref= "url" There is no action element in Angularjs, so I use the ng-submit element.

How / where to define ui-sref ?

form usually has the following code. But the "url" below is not routed through ui-router.

 form(name="LNquestionForm", action="/url") 

I want to do something like

 form(name="LNquestionForm", ng-submit='ui-sref = "url" ') 

So, routing happens through myAngularApp.js as: -

 var myApp = angular.module('myApp', ['ui.router']); myApp.config(function($stateProvider, $urlRouterProvider) { // Now set up the states $stateProvider .state('/url', { url: "/url", templateUrl: "partials/someLink" } }) 
+6
source share
1 answer

Not quite sure that I understood your question correctly, but from what I understand, you want to submit a form and redirect to the given state of your $ statePorivider without any server call. If so, you can easily do something like this:

 <form ng-submit="onFormSubmit" name="myForm"> <input type="text" required/> .... </form> 

In the controller associated with your form, you must:

 $scope.onFormSubmit = function () { if(myForm.$valid) { $state.href('/url' [, optional parameters if needed]); } } 

Be careful that the onFormSubmit function works, you need to enter the public service in your controller or transfer it to your application controller.

Also note that in the example I added some form validation.

+10
source

Source: https://habr.com/ru/post/954034/


All Articles