Issue
I am trying to make the button non-functional after the number of clicks are equal to the value entered in the text boy.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Student <input type="input" ng-model="number"> <br>
<p> {{number}} </p>
<button ng-click= "myFunction()">Click Me!</button>
<p>{{ count }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count=0;
$scope.number;
$scope.myFunction = function() {
$scope.count++;
$scope.count <= $scope.number;
}
});
</script>
</body>
</html>
Solution
If you're looking for a Angular 2+ implementation, you could use two-way binding to ngModel
directive. Try the following
Controller
export class AppComponent {
buttonCount = 0;
studentCount = 0;
onClick() {
this.buttonCount++;
}
}
Controller
Student <input [(ngModel)]="studentCount" (keyup)="buttonCount = 0" type="input"> <br>
Number input: {{ studentCount }}
<br><br>
<button [disabled]="buttonCount >= studentCount" (mouseup)="onClick()">Click me</button>
<br>
Button clicks: {{ buttonCount }}
The (keyup)="buttonCount = 0"
event in the input
tag resets the button click count everytime a value is entered in the input.
Working example: Stackblitz
If you're new to Angular, I'd recommend you to go through this tutorial. It introduces some of the basics.
Answered By - Michael D
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.