0

While clicking a button am calling a service method which will return an integer value. Now my need is, on page load i should disable this button if this integer value is 0. i don't want to use ng-disabled, because then it will keep on calling that service method. any better way?

1
  • 2
    I don't understand why you don't want to use ng-disabled... it won't keep calling the method, simply assign the expression to $scope.integerVar > 0 where integerVar holds the response int. Commented Mar 6, 2015 at 14:27

1 Answer 1

1

I don't know if I get the question right but here is what i did in plunker

<!DOCTYPE html>
<html ng-app="sample">

<head>
  <script data-require="angular.js@*" data-semver="1.4.0-beta.5" src="https://code.angularjs.org/1.4.0-beta.5/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>
<script>
  var module = angular.module("sample", []);
  module.controller('ctrl', function($scope) {
    //assuming that this variable came from your service
    var i = 0;

    $scope.disable = true;
    $scope.init = function() {
      $scope.disable = i === 0 ? true : false;
    }

  });
</script>

<body ng-controller="ctrl" ng-init="init()">
  <button ng-disabled="disable">hello world</button>
</body>
</html>

http://plnkr.co/edit/p9qZGcO1znOiDidecFYd

but basically you really need to use ng-disabled and call the ng-init function so that when your page load, it will check if the value of integer is 0 then flag the button to be disabled or not.

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.