Dosti
Dosti

Reputation: 163

Disable button onload angularJS

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?

Upvotes: 0

Views: 2030

Answers (1)

Francis
Francis

Reputation: 103

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.

Upvotes: 1

Related Questions