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

时间:2015-04-08 22:47:33

标签: html json angularjs angularjs-ng-click ng-bind-html

我在AngularJS领域很新,当然我正在以错误的方式做事。所以,这是我的问题:我有一个小聊天小部件,它通过PHP API从JSON获取数据。在我的JSON中,我提供了包装器和一些ng *标签的所有消息。我遇到的问题是不会对这些元素触发ng-click操作。 html块注入了ng-bind-html。

这是我的角色应用程序:

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!");
            });
    };
});

这是来自JSON的html代码:

<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>

单击元素时ng-click="messageAdminTools('1')不会触发。我做错了什么?

谢谢!

编辑:工作代码

以下是在答案之后修改的代码,解决问题的代码:

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>

1 个答案:

答案 0 :(得分:6)

试试这个:angular ng-bind-html and directive within it

查看vkammerer提供的指令。 特别要注意$compile步骤。