Replace String in AngularJs


Replace String in AngularJs- It is very simple to replace a string in AngularJs. You can use replace method same as we use in JavaScript. Here in this tutorial we are going to explain how you can use replace() method to replace string in AngularJs. You can use our online demo try and edit the code online.


How to Replace String in AngularJs | Example

You can use replace() method to replace string. Here is full example –

How to Replace String in AngularJs | 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.exampleString = "Hello World !";
 $scope.myString = '';
 $scope.replaceStr = function() {
	 // get & replce the input value
	 $scope.exampleString = $scope.exampleString.replace("World", $scope.myString);
	 
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController"> 
 
 Enter a value and click "Replace" Button to replace the - "World".<br>
 <input type="text" name="strExample" ng-model="myString"><br>
 <button ng-click='replaceStr()' >Replace</button><br>
String = {{exampleString}}<br>

</div> 
</div>
</body>  
</html>  

Try it »

In the above example we have created a simple example to replace the string from string in AngularJs.

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

How to Replace String in AngularJs | Example


Advertisements

Add Comment

📖 Read More