简体   繁体   English

使用指令注入DOM元素AngularJS

[英]Injecting DOM elements AngularJS using Directives

I have a <ul> as such with <li> 's: 我也有一个<ul><li>

    <li ng-repeat="x in xs" ng-class-odd="'x'" ng-class-even="'y'">
    // inject here
        <span>
            {{x.v}}
        </span>
    </li>

I'd like on a certain event to inject a context menu (DOM position described above) that looks like this: 我想在某个事件中注入如下所示的上下文菜单(上述DOM位置):

        <ul id="context" class="col">
            <li class="three"><span>1</span></li>
            <li class="three"><span>2</span></li>
            <li class="three"><span>3</span></li>
        </ul>

What's the best way to implement this? 实现此目的的最佳方法是什么? 1, 2 and 3 above have the same functions to handle the repeated list items in the parent container. 上面的1、2和3具有相同的功能来处理父容器中的重复列表项。 So I'm not sure if injecting the context menu as described above is a smart idea since It would generate unseeded repetitions of the context menu. 因此,我不确定如上所述注入上下文菜单是否明智,因为这会生成上下文菜单的非种子重复。

Thanks. 谢谢。

Here's a really basic example of a set of contextmenu directives where menu is inserted once in body. 这是一组contextmenu指令的非常基本的示例,其中菜单一次插入到正文中。

One directive is used to bind the contenxtmenu event and send data to the directive that controls the menu itself. 一个指令用于绑定contenxtmenu事件,并将数据发送到控制菜单本身的指令。

The item selected and the mouse position get passed as data in the broadcast 所选项目和鼠标位置作为数据在广播中传递

HTML HTML

  <ul>
    <li ng-repeat="item in items" context="item">{{item.title}}</li>
  </ul>

  <ul menu id="context" ng-show="menu_on">
    <li ng-click="itemAlert('id')">Alert ID</li>
    <li ng-click="itemAlert('title')">Alert Title</li>
  </ul>

JS JS

app.directive('context', function($rootScope) {
  return {
    restrict: 'A',
    scope: {
      item: '=context'
    },
    link: function(scope, elem) {
      elem.bind('contextmenu', function(evt) {
        evt.preventDefault();
        var data = {
          pos: { x: evt.clientX, y: evt.clientY},
          item: scope.item
        }
        scope.$apply(function(){
          $rootScope.$broadcast('menu', data);
        });

      });
    }
  }
})

 app.directive('menu', function($rootScope) {
  return {
    restrict: 'A',
    link: function(scope, elem) {
      scope.$on('menu', function(evt, data) {
        scope.menu_on = true;
        scope.item = data.item;
        var cssObj = {
          left: data.pos.x + 20 + 'px',
          top: data.pos.y - 40 + 'px'
        };
        elem.css(cssObj)
      });
      scope.itemAlert = function(prop) {
        scope.menu_on = false;
        alert(scope.item[prop])
      }
    }
  }
});

Would need some additional document listeners to close menu when user clicks outside of it. 当用户在菜单外单击时,将需要一些其他文档侦听器来关闭菜单。 Objective here was to just create basic mechanics of being able to display menu and pass data. 这里的目标是创建能够显示菜单和传递数据的基本机制。

I haven't looked but there are probably some open source directives already available that are far more advanced than this. 我没有看过,但是可能已经有一些开源指令比这更先进。

DEMO DEMO

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

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