简体   繁体   English

在ng-click中使用“this”

[英]Use of “this” in ng-click

I am looking at a component of Angular UI Tree . 我正在研究Angular UI Tree的一个组件。 The specific code I am looking at is: 我正在查看的具体代码是:

<a class="pull-right btn btn-primary btn-xs" data-nodrag ng-click="newSubItem(this)" style="margin-right: 8px;">

The line all makes perfect sense to me, except for the use of this for the ng-click. 该行一切使我感觉良好,除了使用this为NG-点击。 I am familiar with the use of this in Javascript and how it differs from the use of self in classical OO. 我熟悉使用this JavaScript和如何它不同于利用self在传统的面向对象的。 But I am wondering what exactly is being referred to when passed to this function call? 但我想知道传递给这个函数调用时究竟是什么?

The code is contained in this basic example . 代码包含在此基本示例中

Well, In here this refers to the scope and it will include every variable, functions which inside of it. 好吧,在这里, this指的是范围,它将包括每个变量,其中的函数。

Wherever we use this, it will always return scope. 无论我们在哪里使用它,它总是会返回范围。

Here, I am passing this from parent and child, but both shows same output as $SCOPE 在这里,我从父母和孩子传递这个,但两者都显示与$SCOPE相同的输出

<body ng-controller="AppController">

  <div><a ng-click="aClick(this)">Click a</a>
    <div>
      <a ng-click="bClick(this)">Click b</a>
    </div>
  </div>

  {{aValue}} {{bValue}}
</body>

Please find the working plunker: http://plnkr.co/edit/IdXB1ZtXMpFS3soxcBIz?p=preview 请找到工作的plunker: http ://plnkr.co/edit/IdXB1ZtXMpFS3soxcBIz?p = preview

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

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