禁用按钮onload angularJS

时间:2015-03-06 14:26:05

标签: angularjs

点击按钮时调用将返回整数值的服务方法。 现在我需要的是,在页面加载时我应该禁用此按钮,如果此整数值为0。 我不想使用ng-disabled,因为它会继续调用该服务方法。 有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

我不知道我的问题是否正确,但这是我在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

但基本上你真的需要使用ng-disabled并调用ng-init函数,这样当你的页面加载时,它会检查整数的值是否为0然后标记要禁用的按钮。

相关问题