Angularjs set input value


Angularjs set input value – It is very simple to set the value attribute of the input field. Here in this tutorial we are going to explain how you can set the input field value in angularjs. You can also use our online editor to edit and run the code online.


Angularjs set input value | Example

Let us go step by step to set the input box value example –

JavaScript Part

JavaScript Part Contains the following part as below –

Angularjs set inputbox value Example :

<script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.nameText = '';
 $scope.setInputValue = function() {
     $scope.nameText = "John Dee";
 };
});
</script>

Html Part

Html Part contains the following html as below –

Angularjs set input value example:

<div ng-app="myApp">  
<div ng-controller="myController">  
  Name <input type="text"  name="mytext" ng-model="nameText">
  </br>
  <input type="button" ng-click="setInputValue()" value="Set Text" >
  </br>     
 
</div> 
</div>

Complete Part

Now let us combine Html and JavaScript Together to create full example like this –

Angularjs set input value 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.nameText = '';
 $scope.setInputValue = function() {
     $scope.nameText = "John Dee";
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
        Name <input type="text"  name="mytext" ng-model="nameText"></br>
		<input type="button" ng-click="setInputValue()" value="Set Text" ></br>
</div> 
</div>
</body>  
</html>

Try it »

If you run the above example it will produce output something like this –

Angularjs set input value


Advertisements

Add Comment

📖 Read More