Angular: ng-click on ng-bind-html block not firing

Comforse picture Comforse · Apr 9, 2015 · Viewed 13.6k times · Source

I am quite new in the area of AngularJS and of course I am doing something in the wrong way. So, here is my problem: I have a small chat widget which takes the data through JSON from a PHP API. In my JSON I provide all the messages with wrappers and with some ng* tags. The problem I have is that the ng-click action is not fired on those elements. The html block are injected with ng-bind-html.

Here is my angular app:

var chat = angular.module("chat", ['ngDialog']);

chat.controller('GetChatMessagesController', function ($scope, $http, $timeout, $sce, ngDialog) {

    $scope.messages = "";


    $scope.getData = function() {
        $http.get("/url/to/api")
            .success(function(data, status, headers, config) {
                $scope.messages = data.html;
            }).error(function(data, status, headers, config) {
                //alert("AJAX f   ailed!");
            });
    };

    $scope.getData();

    $scope.getHtml = function(html){
        return $sce.trustAsHtml(html);
    };

    // Function to replicate setInterval using $timeout service.
    $scope.intervalFunction = function(){
        $timeout(function() {
            $scope.getData();
            $scope.intervalFunction();
        }, 5000)
    };


    // Kick off the interval
    $scope.intervalFunction();

    $scope.messageAdminTools = function(message_id)
    {
        console.log("called");
        var template = $scope.adminToolsTemplate(message_id);
        console.log(template);
        ngDialog.open({
            template: template,
            plain: true
        });
    };

    $scope.adminToolsTemplate = function(message_id)
    {
        $http.get("/url/to/api" + message_id)
            .success(function(data, status, headers, config) {
                return data.html;
            }).error(function(data, status, headers, config) {
                //alert("AJAX f   ailed!");
            });
    };
});

And here is the html code that comes from the JSON:

<body ng-controller="GetChatMessagesController" class="ng-scope">
    <div class="messages-container ng-binding" ng-bind-html="getHtml(messages)">
        <div class="message message_1">
            <span class="time">16:33</span>
            <span class="chatbox_user"><a target="_blank" href="url">admin</a>: </span>
            <span class="text">test <a href="javascript:void(0);" class="chat-tools" ng-click="messageAdminTools('1')">x</a></span>
        </div>
    </div>
</body>

The ng-click="messageAdminTools('1') does not fire when I click the element. What am I doing wrong?

Thanks!

EDIT: Working code

Here is the code modified after the answer, code that solves the issue:

var chat = angular.module("chat", ['ngDialog']);

chat.controller('GetChatMessagesController', function ($scope, $http, $timeout, $sce, ngDialog) {

    $scope.messages = "";


    $scope.getData = function() {
        $http.get("/url/to/api")
            .success(function(data, status, headers, config) {
                $scope.messages = data.html;
            }).error(function(data, status, headers, config) {
                //alert("AJAX f   ailed!");
            });
    };

    $scope.getData();

    $scope.getHtml = function(html){
        return $scope.messages;
    };

    // Function to replicate setInterval using $timeout service.
    $scope.intervalFunction = function(){
        $timeout(function() {
            $scope.getData();
            $scope.intervalFunction();
        }, 5000)
    };


    // Kick off the interval
    $scope.intervalFunction();

    $scope.messageAdminTools = function(message_id)
    {
        console.log("called");
        var template = $scope.adminToolsTemplate(message_id);
        console.log(template);
        ngDialog.open({
            template: template,
            plain: true
        });
    };

    $scope.adminToolsTemplate = function(message_id)
    {
        $http.get("/url/to/api")
            .success(function(data, status, headers, config) {
                return data.html;
            }).error(function(data, status, headers, config) {
                //alert("AJAX f   ailed!");
            });
    };
}).directive('compile', ['$compile', function ($compile) {
    return function(scope, element, attrs) {
        scope.$watch(
            function(scope) {
                // watch the 'compile' expression for changes
                return scope.$eval(attrs.compile);
            },
            function(value) {
                // when the 'compile' expression changes
                // assign it into the current DOM
                element.html(value);

                // compile the new DOM and link it to the current
                // scope.
                // NOTE: we only compile .childNodes so that
                // we don't get into infinite loop compiling ourselves
                $compile(element.contents())(scope);
            }
        );
    };
}]);

HTML:

<body ng-controller="GetChatMessagesController" class="ng-scope">
    <div class="messages-container" compile="getHtml(messages)">
        <div class="message message_1 ng-scope">
            <span class="time">16:33</span>
            <span class="chatbox_user"><a target="_blank" href="url">admin</a>: </span>
            <span class="text">test <a href="javascript:void(0);" class="chat-tools" ng-click="messageAdminTools('1')">x</a></span>
        </div>
    </div>
</body>

Answer

HankScorpio picture HankScorpio · Apr 9, 2015

Try this: angular ng-bind-html and directive within it

Take a look inside the directive that vkammerer provided. Especially take note of the $compile step.