2016-11-04 63 views
1

have一個控制器中的三個按鈕,具有ng鍵功能。當這個元素已經被點擊時,我如何禁用點擊? 這裏是例子:禁用ng單擊此元素

<div ng-app="myApp"> 
    <div ng-controller="GreetingController"> 
    <button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button> 
    <button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button> 
    <button ng-click="!IsClickEnable||DoSomethingElse()">xyz</button> 
    </div> 
</div> 

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

myApp.controller('GreetingController', ['$scope', function($scope) { 
    $scope.IsClickEnable = true; 
    $scope.DoSomethingElse = function() { 
    alert('click') 
    $scope.IsClickEnable = false; 
    }; 

}]); 
+0

你wan't禁用每個按鈕被點擊後? –

+0

@NicolasGarnil是的。現在它禁用所有按鈕。 –

+1

你只需要使用'ng-disabled' – Targaryen

回答

-1

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

 
myApp.controller('GreetingController', ['$scope', function($scope) { 
 
    $scope.DoSomethingElse = function(event) { 
 
    event.target.disabled = true; 
 
    }; 
 

 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <div ng-controller="GreetingController"> 
 
    <button ng-click="DoSomethingElse($event)">xyz</button> 
 
    <button ng-click="DoSomethingElse($event)">xyz</button> 
 
    <button ng-click="DoSomethingElse($event)">xyz</button> 
 
    </div> 
 
</div>

2

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

 
myApp.controller('GreetingController', ['$scope', 
 
    function($scope) { 
 

 
    $scope.buttons = [{ 
 
     "name": "xyz", 
 
     "disabled": false 
 
    }, { 
 
     "name": "xyz", 
 
     "disabled": false 
 
    }, { 
 
     "name": "xyz", 
 
     "disabled": false 
 
    }]; 
 

 
    $scope.DoSomethingElse = function(b) { 
 
     console.log("Do something"); 
 
     b.disabled = true; 
 
    } 
 
    } 
 
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <div ng-controller="GreetingController"> 
 
    <button ng-repeat="button in buttons" 
 
      ng-disabled="button.disabled" 
 
      ng-click="DoSomethingElse(button)"> 
 
     {{button.name}} 
 
    </button> 
 
    </div> 
 
</div>

+0

這是太多的代碼來禁用一個按鈕。 – Targaryen

+0

@Targaryen這就是爲什麼你倒下了它?我嘗試在不處理DOM元素的情況下提出替代方案。這是行之有效的 – Weedoze

+0

它是'n​​g-disabled'(不是ng-disable),這就是你需要的全部 – Targaryen

1

只需添加ND-禁用指令你的按鈕:

<button ng-click="clicked=true;DoSomethingElse()" ng-disabled-"clicked">xyz</button> 

一個獨立於JavaScript的解決方案。這種方式您不需要關心在您的控制器中設置標誌。只需在您的控制器初始化時添加clicked=false

+0

在這種情況下,以後不再點擊按鈕。 'clicked'變量需要以角度存儲。 – Targaryen

+1

是的'clicked = false'初始化將在控制器中處理它。 – Rambler

1

更好的方法是編寫一個自定義指令,使用jqLit​​e或jQuery(在加載jQuery庫的情況下)更新DOM元素。檢查下面的代碼片段。

DOM不應該在控制器中更新,因爲它禁止在指令中發生單元測試和DOM操作,在這種情況下證明按鈕是在視圖本身中創建的,而不是使用控制器。

angular 
 
    .module('demo', []) 
 
    .controller('GreetingController', GreetingController) 
 
    .directive('disableOnClick', disableOnClick); 
 

 
GreetingController.$inject = ['$scope']; 
 

 
function GreetingController($scope) { 
 
    $scope.doSomethingElse = function() { 
 
    console.log('do something else'); 
 
    } 
 
} 
 

 
function disableOnClick() { 
 
    var directive = { 
 
    restrict: 'A', 
 
    link: linkFunc 
 
    } 
 

 
    return directive; 
 

 
    function linkFunc(scope, element, attrs, ngModelCtrl) { 
 
    element.on('click', function(event) { 
 
     event.target.disabled = true; 
 
    }); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script> 
 
<div ng-app="demo"> 
 
    <div ng-controller="GreetingController"> 
 
    <button disable-on-click ng-click="doSomethingElse()">xyz</button> 
 
    <button disable-on-click ng-click="doSomethingElse()">xyz</button> 
 
    <button disable-on-click ng-click="doSomethingElse()">xyz</button> 
 
    </div> 
 
</div>

+1

OP問javascript不是JQuery – Weedoze

+1

@Weedoze,我的代碼片段不使用jQuery。 AngularJS自帶jQuery內置jQuery的裁剪版本,使用jqLit​​e的API我已經在按鈕上映射了一個click事件來禁用它們 –