简体   繁体   English

指令和元素间的沟通

[英]directives and inter-element communication

I created the following simple directive to redirect enter keypress events. 我创建了以下简单指令来重定向输入按键事件。

I think it might be a better to use emit a message to another directive, that handles the keypress event, rather than using a jQuery identifier, as I do here. 我认为最好使用将消息发送到另一个处理keypress事件的指令,而不是像我在这里使用jQuery标识符那样使用。

Consistent with Angular philosophy, what is best practice when dealing with element to element communication? 与Angular哲学相一致,处理元素到元素的交流时的最佳实践是什么? why? 为什么?

thanks 谢谢

=========================================================
.directive('redirectEnter',function() {
    return {
        restrict : 'A',
        link : function($scope,$element,$attr) {
            $element.keypress(function($event) {
                if($event.keyCode == '13') {
                    $($attr.redirectEnter).click();
                    $event.stopPropagation();
                    $event.preventDefault();
                }
            });
        }
    }
})
=========================================================
<a redirect-enter="#apply">Redirect enter example</a>
<button id="apply">Apply</button> 
=========================================================

Your HTML example doesn't have any logic wired to the button element via ng-click , etc., so for the sake of example, let's imagine a view like this: 您的HTML示例没有通过ng-click等连接到button元素的任何逻辑,因此为示例起见,让我们想象一下这样的视图:

<input ng-model="model.something">
<button ng-click="submit()">

(Remember, your DOM elements should be tied to logic via directives or a controller's scope. Using $("#apply").click(...) is a no-no!) (请记住,您的DOM元素应该通过指令或控制器的作用域与逻辑绑定。使用$("#apply").click(...)是不行的!)

In this example, it's easy to say that we want the input element to eat "enter" keypresses and "redirect" them to the button. 在此示例中,很容易说我们希望input元素吃掉“输入”按键并将它们“重定向”到按钮。 But what we really want an "enter" keypress in the input to do is the action the button would otherwise perform --in this case, calling the submit method on the scope. 但我们真正想要的是“输入”按键input要做的是按钮将 --in这种情况下,否则执行的操作 ,调用submit的范围方法。

Here is a directive that takes an expression to evaluate as its redirect-enter attribute (I'd probably call it onEnter or something) and evaluates the expression. 这是一条指令,该指令将表达式作为其redirect-enter属性(我可能将其onEnter )进行评估,并对表达式进行评估。

<input ng-model="model.something" redirect-enter="submit()">
<button ng-click="submit()">Submit</button>

Here is the directive that makes this work: 这是使这项工作有效的指令:

app.directive('redirectEnter', function() {
  return {
    restrict : 'A',
    link : function($scope,$element,$attr) {
      $element.keypress(function($event) {
        if($event.keyCode == '13') {
          $scope.$eval($attr.redirectEnter);
          $event.stopPropagation();
          $event.preventDefault();
        }
      });
    }
  }
});

Here is an example on jsFiddle: http://jsfiddle.net/BinaryMuse/HgD9y/ 这是jsFiddle上的示例: http : //jsfiddle.net/BinaryMuse/HgD9y/

Note that, if you want the same behavior on a link element, you should use ng-click . 请注意,如果您希望link元素具有相同的行为,则应使用ng-click

If you're struggling with keeping yourself from using plain jQuery selectors in your Angular code (like the no-no I mentioned at the top of this answer), consider this advice from the Angular FAQ : 如果您想避免自己在Angular代码中使用普通的jQuery选择器(例如我在此答案顶部提到的不行),请考虑Angular FAQ中的以下建议:

Stop trying to use jQuery to modify the DOM in controllers. 停止尝试使用jQuery修改控制器中的DOM。 Really. 真。 That includes adding elements, removing elements, retrieving their contents, showing and hiding them. 这包括添加元素,删除元素,检索其内容,显示和隐藏它们。 Use built-in directives, or write your own where necessary, to do your DOM manipulation. 使用内置指令,或在必要时编写自己的指令来进行DOM操作。 See below about duplicating functionality. 有关复制功能,请参见下文。

If you're struggling to break the habit, consider removing jQuery from your app. 如果您想改掉习惯,请考虑从应用程序中删除jQuery。 Really. 真。 Angular has the $http service and powerful directives that make it almost always unnecessary. Angular具有$ http服务和功能强大的指令,使其几乎总是不必要的。 Angular's bundled jQLite has a handful of the features most commonly used in writing Angular directives, especially binding to events. Angular捆绑的jQLite具有一些在编写Angular指令时最常用的功能,尤其是绑定到事件。

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

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