原文: https://thinkster.io/egghead/index-event-log

--------------------------------------------------------------

$index is a way to show which iteration of a loop you’re in. If we set up an ng-repeat to repeat over the letters in ‘somewords’, like so:

<div ng-app="app">
<div ng-repeat="item in 'somewords'.split('')">
{{$index + 1}}. {{item}}
</div>
</div>

We can see that we get a listing of all the characters in ‘somewords’ with the index next to it.

Now, let’s add an ng-click attribute to the div as “ev = $event” and a binding to ev.pageX. Let’s also set this div’s class to “button” since we’re going to be clicking on it

<div ng-app="app">
<div
class="button"
ng-repeat="item in 'somewords'.split('')"
ng-click="ev = $event"
>
{{$index + 1}}. {{item}}
{{ev.pageX}}
</div>
</div>

Now we can click on all of these buttons and whenever we click we get the extra number next to our index and character. This is the x value of where we’re clicking, and this shows that we can access the event that’s happening through $event.

If we want to log this event, we can do so by using $log. In order to use $log without setting up a controller, we can put it on the root scope of our application. We need to set up the run phase of our application and inject $rootScope and $log in order to expose $log to $rootScope

var app = angular.module("app", []);

app.run(function($rootScope, $log){
$rootScope.$log = $log;
});

Now we’re able to access the $log function anywhere within our app. (Note that you rarely want to put anything on the $rootScope as anything on the $rootScope will be available throughout the app.) Let’s change the ng-click attribute to “$log.debug($event)”

<div ng-app="app">
<div
class="button"
ng-repeat="item in 'somewords'.split('')"
ng-click="$log.debug($event)"
>
{{$index + 1}}. {{item}}
{{ev.pageX}}
</div>
</div>

Now we can open up our console and click on any of the buttons and we’ll see that the MouseEvent is being logged to the console. You can turn off the debug level of the logger in the config phase of the app by using $logProvider and calling debugEnabled and passing it false.

var app = angular.module("app", []);

app.config(function($logProvider){
$logProvider.debugEnabled(false);
}); app.run(function($rootScope, $log){
$rootScope.$log = $log;
});

Be sure to use proper logging levels in order for debugEnabled to work, as any info or warnings will still be logged

AngularJS - $index, $event, $log的更多相关文章

  1. log file switch (checkpoint incomplete) - 容易被误诊的event

    本文转自 https://blogs.oracle.com/database4cn/log-file-switch-checkpoint-incomplete-%e5%ae%b9%e6%98%93%e ...

  2. jenkins发送带附件(logfile.log和index.html)的邮件配置

    先进入到job里面,在Attachment中按照规矩添加文件就好了 此处是以workspace作为根目录的,logfile.log文件刚好就在根目录上,所以直接写上,多个文件的话用逗号分隔, 第二个文 ...

  3. python log

    python的日志模块为logging,它可以将我们想要的信息输出保存到一个日志文件中. # cat log import logging logging.debug('This is debug m ...

  4. index index.html index.htm index.php

    server { listen 80; server_name localhost; index index.html index.htm index.php;#前后顺序有关系,越在前优先级别越高 r ...

  5. git log用法【转】

    转自:http://www.cnblogs.com/gbyukg/archive/2011/12/12/2285419.html PHP技术交流群 170855791 git log 查看提交记录,参 ...

  6. log file sync

    Recently, our application system has updated one app. I receive a email of complain the db server ch ...

  7. [转][Angularjs]$http.post与$.post

    本文转自:https://www.cnblogs.com/wolf-sun/p/6878868.html 摘要 在angularjs发送post请求的时候,确实很困惑,在传递json数据的时候,总会遇 ...

  8. MySQL启动报错Failed to open log (file 'D:\phpStudy\PHPTutorial\MySQL\data\mysql_bin.000045', errno 2)

    MySQL报错 191105 9:39:07 [Note] Plugin 'FEDERATED' is disabled. 191105 9:39:07 InnoDB: The InnoDB memo ...

  9. 如何正确使用日志Log

    title: 如何正确使用日志Log date: 2015-01-08 12:54:46 categories: [Python] tags: [Python,log] --- 文章首发地址:http ...

  10. UWP开发之Mvvmlight实践七:如何查找设备(Mobile模拟器、实体手机、PC)中应用的Log等文件

    在开发中或者后期测试乃至最后交付使用的时候,如果应用出问题了我们一般的做法就是查看Log文件.上章也提到了查看Log文件,这章重点讲解下如何查看Log文件?如何找到我们需要的Packages安装包目录 ...

随机推荐

  1. javascript.json snippets vscode 注释

    vscode vue js里面的注释 javascript.json { // Place your global snippets here. Each snippet is defined und ...

  2. HTML 5 <a> 标签

    href 属性 定义和用法 href 属性规定链接的目标地址. 如果未使用 href 属性,则 <a> 标签不是链接,而是链接的占位符. HTML 4.01 与 HTML 5 之间的差异 ...

  3. OpenCV:应用篇

    手势跟踪识别 车牌检测 人脸识别 去雾 图像阈值分割提取

  4. Thread和Runable的关系

    Thread 是一个类 Runnable是一个接口 Thread是实现了Runnable接口的类,使得run支持多线程 因为类的单一继承原则,推荐多使用Runnable接口

  5. ios字体简单设定

    UILabel *lable = [[UILabel alloc] init]; label.font = [];

  6. 717. 1-bit and 2-bit Characters@python

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  7. STL源码分析与实现-stl_list容器

    1. stl_list 介绍 今天我们来总结一下stl_List, 通过之前介绍单链表的文章,其实对链表的基本操作已经十分熟悉了,那对于stl_list,无非就是链表结构不一样,至于其中的增删改查的细 ...

  8. [kuangbin带你飞]专题五 并查集

    并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...

  9. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  10. c++值传递和引用及指针传递区别

    以下程序各有何问题? ***************************************************************************************** ...