简体   繁体   English

AngularJS使用JavaScript将指令动态添加到DOM

[英]AngularJS adding Directive dynamically to DOM with JavaScript

I am developing a small chat application using Angular 1.x and Socket.io. 我正在使用Angular 1.x和Socket.io开发一个小型聊天应用程序。 There is a list showing available people for chatting on the screen. 屏幕上会显示一个列表,其中列出了可以聊天的人。 When clicked on a person, in the controller a chat function called. 当单击某个人时,在控制器中称为聊天功能。 I pass the required person info (id, socket_id etc.). 我传递了所需的人员信息(id,socket_id等)。 Then I create a chat box like facebook chat box with directive dynamically and add to dom. 然后,我使用指令动态创建一个聊天框(如facebook聊天框)并添加到dom。 Directive has an isolated scope. 指令具有孤立的范围。 This is code to add directive to DOM when clicked on a person: 这是在单击人员时向DOM添加指令的代码:

temp = {
'name' : driver.name,
'surname' : driver.surname,
'image' : driver.image
};
var divElement = angular.element(document.querySelector('.chat-container'));
var appendHtml = $compile('<chat-box receiver="' + temp + 
       '"  id="' + driver.id + '" visible="true"></chat-box>')($scope);
divElement.append(appendHtml);

And the directive code: 和指令代码:

.directive('chatBox', function($timeout) {
    chatboxes = [];
    return {
        restrict : 'EA',
        templateUrl : 'templates/chat.box.tpl.html',
        replace : true,
        scope : {
            receiver : '@',
            visible : '@',
            id : '@'
        },
        link: function(scope, element, attrs) {

        },
        controller : function($scope) {


            $scope.close = function() {
                $scope.visible = false;
            }
        }
    }
})
  1. Problem: I am facing problem getting object data (receiver = temp) in the directive and apply to view. 问题:我在获取指令中的对象数据(接收器=临时)并将其应用于视图时遇到问题。 How can I send an object data and handle in directive properly? 如何发送对象数据并正确处理指令?
  2. Problem: After creating a chat box for a person, I want to prevent creating a second chat for the same person. 问题:在为一个人创建聊天框后,我想防止为同一个人创建第二个聊天室。 How can I handle this? 我该如何处理?

In AngularJS, avoid raw DOM manipulation. 在AngularJS中,避免原始DOM操作。 DOM manipulation should be done only by directives: DOM操作只能通过指令来完成:

<div class="chat-container">
   <chat-box ng-show="$ctrl.visible"
             receiver="$ctrl.temp" 
             id="{{$ctrl.driver.id}}" 
             on-close="$ctrl.visible = false">
   </chat-box>'
</div>

In the directive use one-time < binding to put objects in the isolate scope and use expression & binding to communicate a close event from the directive: 在指令中,使用一次性<绑定将对象置于隔离范围内,并使用表达式&绑定来传递指令中的close事件:

app.directive('chatBox', function() {
    return {
        restrict : 'EA',
        templateUrl : 'templates/chat.box.tpl.html',
        scope : {
            receiver : '<',
            onClose : '&',
            id : '@'
        },
        link: function(scope, element, attrs) {

        },
        controller : function($scope) {

            $scope.close = function() {
                $scope.onClose();
            }
        }
    }
})

In this case the visibility of the element is controlled by the ng-show directive . 在这种情况下,元素的可见性由ng-show指令控制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM