Angularjs get string length
Angularjs get string length – You can get the string length using the length attribute. Here in this tutorial we are going to explain how you can get the length of string variable in angularjs. You can also use our online editor to edit and run the code online.
Angularjs get string length | Example
Let us go step by step to get the string length example –
JavaScript Part
JavaScript Part Contains the following part as below –
Angularjs count String Characters :
<script> var myApp = angular.module("myApp", []); myApp.controller("myController", function($scope) { $scope.length = ''; $scope.getStrLength = function() { $scope.length = $scope.inputText.length; }; }); </script> |
Html Part
Html Part contains the following html as below –
Angularjs count string characters example:
<div ng-app="myApp"> <div ng-controller="myController"> Enter Text = <input type="text" ng-keyup="getStrLength()" name="mytext" ng-model="inputText"></br> <p>Length = {{length}}</p> </div> </div> |
Complete Part
Now let us combine Html and JavaScript Together to create full example like this –
Angularjs count string characters example
<!DOCTYPE html> <html lang="en"> <head> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script> <script> var myApp = angular.module("myApp", []); myApp.controller("myController", function($scope) { $scope.length = ''; $scope.getStrLength = function() { $scope.length = $scope.inputText.length; }; }); </script> </head> <body> <div ng-app="myApp"> <div ng-controller="myController"> Enter Text = <input type="text" ng-keyup="getStrLength()" name="mytext" ng-model="inputText"></br> <p>Length = {{length}}</p> </div> </div> </body> </html> |
If you run the above example it will produce output something like this –
Advertisements