3

I'm using ng-disabled in my view like this I have truthy conditions

 md-button.md-primary.md-raised flex="20" ng-disabled="form.$invalid || form.$pristine || day == 0 || leaveapplication.reason == null" ng-click="save(starts_on, ends_on, leave_type_id, period1, period2)" Apply

So I'm gonna use function in my ng-disabled this time:

 md-button.md-primary.md-raised ng-disabled="buttonChecker()" ng-click="save(starts_on, ends_on, leave_type_id)" Apply

Now how can I put my condition in my function like form.$valid and form.$pristine the other condition is easy like day == 0, but the other conditions are hard for me.

4
  • Not able to follow your requirement or issue Commented Apr 17, 2017 at 10:13
  • are you asking, how would you be ale to access form from function ? Commented Apr 17, 2017 at 10:14
  • @SachetGupta yes Commented Apr 17, 2017 at 10:21
  • access it directly from $scope, ex: $scope.formName Commented Apr 17, 2017 at 10:22

2 Answers 2

2

Just access it with $scope where the name of the form is binded to $scope.nameOfForm. I don't know where your other variables like day or leaveapplication.reason are defined so I just inserted them blankly. You have to edit this part to make it work but I think this will show you how to achieve what you trying to.

View

<div ng-controller="MyCtrl">
  <form name="myForm">
      <input type="text" name="test" ng-model="test">
      <button ng-disabled="buttonChecker();">
        Some Button
      </button>
  </form>
</div>

AngularJS Application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.buttonChecker = function () {
      return $scope.myForm.$invalid 
                || $scope.myForm.$pristine 
                || $scope.day === 0 
                || $scope.leaveapplication.reason === null;
    };
});

> Demo fiddle

Sign up to request clarification or add additional context in comments.

12 Comments

let me try this
Myform is the name of the form??
Yea, as you can see in the HTML markup. <form name="myForm">
he cant read my function when a load the page its already enable not disabled
yes i will mark this right if it is working do u have plunker?
|
0

Try passing the form on your function.

 md-button.md-primary.md-raised ng-disabled="buttonChecker(formName)" ng-click="save(starts_on, ends_on, leave_type_id)" Apply

and have the buttonChecker check the form using

$scope.buttonChecker = function (formName) {
  return formName.$invalid || formName.$pristine;      
};

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.