How to empty the content of an element using AngularJS ?
Here the task is to remove the content of an element in HTML DOM with the help of AngularJS.
Approach: First select the element whose content is to be emptied. Then use the empty() method to remove the content of the element.
Example 1: In this example the content of element of ID(‘div’) is emptied.
<!DOCTYPE HTML> <html> <head> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js" > </script> <script> var myApp = angular.module( "app" , []); myApp.controller( "controller" , function ($scope) { $scope.emptyEl = function () { var el = angular.element( document.querySelector( '#div' )); el.empty(); }; }); </script> <style> #div { height : 80px ; width : 170px ; margin : 0 auto ; background : green ; color : white ; } </style> </head> <body style= "text-align:center;" > <h 1 style= "color:green;" > GeeksForGeeks </h 1 > <p> Empty the content of an element </p> <div id= "div" > GeeksForGeeks <br> <br> A Computer Science portal </div> <br> <div ng-app= "app" > <div ng-controller= "controller" > <input type= "button" value= "Click here" ng-click= "emptyEl()" > </div> </div> </body> </html> |
Output:
Example 2: In this example the content of an element along with its children are emptied.
<!DOCTYPE HTML> < html > < head > < script src = "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js" > </ script > < script > var myApp = angular.module("app", []); myApp.controller("controller", function ($scope) { $scope.emptyEl = function () { var el = angular.element( document.querySelector('#div')); el.empty(); }; }); </ script > < style > #div { height: 80px; width: 170px; margin: 0 auto; background: green; color: white; } #div2 { background: blue; margin: 10px; } </ style > </ head > < body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < p > Empty the content of an element </ p > < div id = "div" > < h4 >This is parent element</ h4 > < div id = "div2" >This is child element</ div > </ div > < br > < div ng-app = "app" > < div ng-controller = "controller" > < input type = "button" value = "Click here" ng-click = "emptyEl()" > </ div > </ div > </ body > </ html > |
Output: