Angularjs Form validation How can i get values inside Controller

Issue

This is my Html Part

    <form name="userForm" ng-submit="submitForm()" novalidate>

                        <!-- NAME -->
                        <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
                            <label>Name</label>
                            <input type="text" name="name" class="form-control" ng-model="user.name" required>
                            <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">You name is required.</p>
                        </div>
  <button type="submit" ng-disabled="userForm.$invalid">Submit</button>
</form>

Controller

 $scope.submitForm = function (userForm) {
        $scope.master = angular.copy(userForm);
        if ($scope.userForm.$valid) {
            debugger;
            console.log($scope.userForm.name);
            alert('our form is amazing');
            console.log($scope.userForm);
        }

    };

Here why i’m not able to get values from Html to Controller

Solution

DO NOT define parameter for function. Use $scope.user in controller if you want the value of user and for getting form, use $scope.userForm in your controller.

$scope.submitForm = function () {
   $scope.master = angular.copy($scope.userForm);
  .
  .
  .
}

Answered By – samira mokaram

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.