最新的Web開發教程
 

AngularJS NG-點擊指令


1增加計數變量,每個按鈕被點擊時:

<button ng-click="count = count + 1" ng-init="count=0">OK</button>
試一試»

定義和用法

ng-click指令告訴AngularJS點擊一個HTML元素時該怎麼做。


句法

< element ng-click=" expression "></ element >

所有HTML元素的支持。


參數值

Value Description
expression An expression to execute when an element is clicked.

更多示例

執行一個功能,AngularJS,一個按鈕被點擊時:

<body ng-app="myApp">

<div ng-controller="myCtrl">
    <button ng-click="myFunc()">OK</button>
    <p>The button has been clicked {{count}} times.</p>
</div>

<script>
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
    $scope.count = 0;
    $scope.myFunc = function() {
        $scope.count++;
    };
}]);
</script>

</body>
試一試»