9951 explained code solutions for 126 technologies


angularjsHow can I use AngularJS to manipulate the innerHTML of an element?


AngularJS provides several methods to manipulate the innerHTML of an element. One of the most commonly used methods is the innerHTML property. This property allows you to set the innerHTML of an element directly. For example:

<div id="exampleDiv">
    <p>Hello World!</p>
</div>

<script>
    document.getElementById("exampleDiv").innerHTML = "<p>Goodbye World!</p>";
</script>

This code will replace the innerHTML of the exampleDiv element with the new HTML string <p>Goodbye World!</p>.

Another way to manipulate the innerHTML of an element is to use the innerHTML property of the AngularJS $element service. This service allows you to access the underlying DOM element of an AngularJS element. For example:

<div ng-controller="ExampleController">
    <div>{{message}}</div>
</div>

<script>
    angular.module('exampleApp', [])
        .controller('ExampleController', function($scope, $element) {
            $element.find('div').eq(0).innerHTML = "Goodbye World!";
        });
</script>

In this example, the $element service is used to find the div element inside the ExampleController and then the innerHTML property is used to set the HTML to Goodbye World!.

Finally, AngularJS also provides the $interpolate service which allows you to evaluate an expression and set the innerHTML of an element. For example:

<div ng-controller="ExampleController">
    <div>{{message}}</div>
</div>

<script>
    angular.module('exampleApp', [])
        .controller('ExampleController', function($scope, $interpolate) {
            var html = $interpolate("Goodbye {{name}}!")({name: "World"});
            $element.find('div').eq(0).innerHTML = html;
        });
</script>

In this example, the $interpolate service is used to evaluate the expression Goodbye {{name}}! and set the innerHTML of the div element to Goodbye World!.

You can also use the ng-bind-html directive to bind an expression to the innerHTML of an element. For example:

<div ng-controller="ExampleController">
    <div ng-bind-html="message"></div>
</div>

<script>
    angular.module('exampleApp', [])
        .controller('ExampleController', function($scope) {
            $scope.message = "Goodbye World!";
        });
</script>

In this example, the ng-bind-html directive is used to bind the message expression to the innerHTML of the div element, resulting in the innerHTML being set to Goodbye World!.

These are just a few of the ways to manipulate the innerHTML of an element using AngularJS. For more information, please see the following links:

Edit this code on GitHub