Can I trigger a form submit from a controller?

后端 未结 6 1490
独厮守ぢ
独厮守ぢ 2020-12-05 00:04

I\'d like to do a traditional form submit from within a controller. The scenario is that I want to hit a route on my web server and redirect to its response, which I can do

6条回答
  •  鱼传尺愫
    2020-12-05 00:53

    Did you try to use the ng-submit directive on your form? You may return true/false after your validation.

    Controller

    app.controller('MainCtrl', ['$location', function($scope, $location) {
      $scope.submit = function(user) {
        var isvalid = true;
        // validation 
        if (isvalid) {
            $http.get('api/check_something', {}).then(function(result) {
                $location.path(result.data);
            });
            return true;
        }
        return false; //failed
      }
    });
    

    Html (you must not have an action attribute)

提交回复
热议问题