0

I'm using AngularJS to validate a form, and I have this button to submit.

<button type="submit" ng-disabled="btn" ng-click="submit(settings.$valid)"
        class="btn btn-primary">
  Submit
</button>

This button is disabled whenever the form is invalid, but once it's valid, the button is enabled.

So this is my code

$http.get("http://localhost:5000/settings").then(function(response){
    $scope.numberOfRows = response.data.numberOfRows
    console.log($scope.numberOfRows)
    if($scope.numberOfRows==0 && $scope.settings.$valid == false ){
        $scope.btn=true
    }else if($scope.numberOfRows==0 && $scope.settings.$valid == true){
        $scope.btn=false
    }

    if($scope.numberOfRows==1){
        $scope.btn=true
    }

})

My problem is when the numberOfRows = 0 and the form is invalid, I can't submit the form which what I want.

BUT, when I fill my form and it became valid, nothings happend.

Can you help me ?

8
  • What do you mean by nothing happened? can you explain in more detail? Commented Sep 1, 2017 at 20:07
  • I mean, whenever i fill the form correctly, the button still disabled and I can't submit the form Commented Sep 1, 2017 at 20:11
  • You can debug this just do {{btn}} in the html, and do console.log($scope.numberOfRows, $scope.settings.$valid) inside the function you gave! Commented Sep 1, 2017 at 20:13
  • I tried {{btn}} but always true, and for the console.log(), it can't show you another result unless you refresh the page, it means you'll begin always at the same point Commented Sep 1, 2017 at 20:16
  • What you need to do, is give ng-disabled="validate()" for the button and move the if statements inside the validate function, this will calculate continuously, I think that your function is checking the conditions only once. Commented Sep 1, 2017 at 20:21

1 Answer 1

1
$http.get("http://localhost:5000/settings").then(function(response){
    $scope.hasRow= response.data.numberOfRows>0;
    $scope.settings.$valid=$scope.hasRow;
})

and add this code to your html

 <button type="submit" ng-disabled="!settings.$valid" ng-click="submit()"
        class="btn btn-primary">
  Submit
</button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.