1

I'm very new to angularjs.

I'm trying to display HTML content on the view using AngularJs. I used ng-model but this is showing HTML contents as string there on view.

Now I'm trying ng-bind-html, this is working for the first time. I'm Trying to append the data dynamically. But data is not coming on view.

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/angular.js" type="text/javascript"></script>
        <script src="node_modules/angular-sanitize/angular-sanitize.js" type="text/javascript"></script>
    </head>
    <body>
        <div style="float: right; width: 60%; height: 500px;" ng-app="myApp" ng-controller="myCtrl">
            <div ng-bind-html="chatdata"></div>
        </div>

        <script>
            var app = angular.module("myApp", ['ngSanitize']);

            var a = "";

            app.controller('myCtrl', function ($scope, $sce) {

                a += "<div style='float: left; width: 70%'>p1 : data1</div>";

                $scope.chatdata = $sce.trustAsHtml(a);

                console.log(a);

                var i = 2;

                var interval = setInterval(function () {

                    if(i === 22 ) {
                        clearInterval(interval);
                    }

                    if (i % 2 === 0) {

                        a = a + "<div style='float: right; width: 70%'>p2 : data" + i +"</div>";
                        $scope.chatdata = $sce.trustAsHtml(a);
                    } else {

                        a = a + "<div style='float: left; width: 70%'>p1 : data" + i +"</div>";

                        $scope.chatdata = $sce.trustAsHtml(a);
                    }
                    console.log(a);
                    console.log($scope.chatdata);
                    i++;
                }, 1000);
            });

        </script>

    </body>
</html>

As you can see in the screenshot data is appending only once.

enter image description here

0

3 Answers 3

2

Instead of using $scope.$apply() and run the complete digest cycle, use

$scope.$apply(function(){
  $scope.chatdata
})

Which runs digest cycle for only chatdata

Running $scope.$apply() i,e complete digest cycle affects the performance of the application.

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.4/angular-sanitize.js" type="text/javascript"></script>
    </head>
    <body>
        <div style="float: right; width: 60%; height: 500px;" ng-app="myApp" ng-controller="myCtrl">
            <div ng-bind-html="chatdata"></div>
        </div>

        <script>
            var app = angular.module("myApp", ['ngSanitize']);

            var a = "";

            app.controller('myCtrl', function ($scope, $sce) {

                a += "<div style='float: left; width: 70%'>p1 : data1</div>";

                $scope.chatdata = $sce.trustAsHtml(a);

                console.log(a);

                var i = 2;

                var interval = setInterval(function () {

                    if(i === 22 ) {
                        clearInterval(interval);
                    }

                    if (i % 2 === 0) {

                        a = a + "<div style='float: right; width: 70%'>p2 : data" + i +"</div>";
                        $scope.chatdata = $sce.trustAsHtml(a);
                    } else {

                        a = a + "<div style='float: left; width: 70%'>p1 : data" + i +"</div>";

                        $scope.chatdata = $sce.trustAsHtml(a);
                    }
                    console.log(a);
                    console.log($scope.chatdata);
                    $scope.$apply(function(){
                      $scope.chatdata
                    })
                    i++;
                }, 1000);
            });

        </script>

    </body>
</html>

Please run the above SNIPPET

HERE IS A WORKING DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

Try to Use $scope.$apply(); after assigning value to the chat data at the end of your function.

1 Comment

I'll try and let you know. Thanks for the quick response.
1

Inject $timeout in your controller and use it when assigning $scope.chatdata value to tell angular a scope has been updated

$timeout(function() {
    $scope.chatdata = $sce.trustAsHtml(a);
});

Working fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.