单击AngularJS切换字符串

时间:2014-05-02 20:18:26

标签: angularjs angularjs-ng-click

<button id="visibilityStatus" ng-model="visibilityStatus" ng-click="visibilityEnable()">Enable</button>

$scope.visibilityEnable = function() {
    document.getElementById('visibilityStatus').value(document.getElementById('visibilityStatus').value == 'Enabled' ? 'Disabled' : 'Enabled');
}

使用AngularJS我在点击button时尝试在2个值之间切换,我尝试将其切换为EnabledDisabled

提前致谢

2 个答案:

答案 0 :(得分:3)

在控制器中,您只需设置要显示的值,然后让HTML模板呈现您的值。

将您的标记更改为:

<button ng-model="visibilityStatus" ng-click="visibilityEnable()" ng-bind="buttonText"></button>

备用标记:

<button ng-model="visibilityStatus" ng-click="visibilityEnable()">{{buttonText}}</button>

在你的控制器中:

$scope.buttonText = 'Enable'
$scope.visibilityEnable = function(){
  $scope.buttonText = $scope.buttonText === 'Enable' ? 'Disable' : 'Enable';
}

答案 1 :(得分:1)

有点简单

<button ng-init="buttonText='Enable'" ng-click="buttonText = buttonText === 'Enable' ? 'Disable' : 'Enable'">{{buttonText}}</button>