前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >angular 常用事件

angular 常用事件

作者头像
kl博主
发布2023-11-17 19:20:14
1340
发布2023-11-17 19:20:14
举报
文章被收录于专栏:kl的专栏

ngClick

适用标签:所有 触发条件:单击

代码语言:javascript
复制
#html 

	"LearnCtrl">
	
		"click()">click me
	
"click()">click me

#script angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) { $scope.click = function () {
                alert('click');
            }
        });

ngDblclick

适用标签:所有 触发条件:双击

代码语言:javascript
复制
#html 

	"LearnCtrl">
	
		"dblclick()">click me
	
"dblclick()">click me

#script angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) { $scope.dblclick = function () {
                alert('click');
            }
        });

ngBlur

适用标签:

  • a
  • input
  • select
  • textarea

触发条件:失去焦点

代码语言:javascript
复制
#html <div ng-controller="LearnCtrl"> <a href="" ng-blur="blur()">linka> <input type="text" ng-blur="blur()"/> <textarea cols="30" rows="10" ng-blur="blur()">textarea> <select ng-blur="blur()"> <option>----option> <option>jackyoption> <option>roseoption> select> div> #script
angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) {
            $scope.blur = function () {
                alert('blur');
            }
        });

ngFocus

适用标签:

  • a
  • input
  • select
  • textarea

触发条件:获取焦点

代码语言:javascript
复制
#html <div ng-controller="LearnCtrl"> <a href="" ng-focus="focus()">linka> <input type="text" ng-focus="focus()"/> <textarea cols="30" rows="10" ng-focus="focus()">textarea> <select ng-focus="focus()"> <option>----option> <option>jackyoption> <option>roseoption> select> div> #script
angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) {
            $scope.focus= function () {
                alert('focus');
            }
        });

ngChange

适用标签:input 触发条件:model更新

输入框的内容改变并不代表model的值更新。按我的理解,一般当两个状态互相切换时,model值会更新。两个状态我称之为合法状态和不合法状态。

不合法的状态:输入的内容不符合type类型,如email类型。输入的内容不符合校验条件,如ngMinlength。不合法的状态下,model会被更新成undefined

合法的状态:输入的内容是符合类型和校验条件的。

代码语言:javascript
复制
#html 

	"LearnCtrl">"text" ng-model="text" ng-change="change()" ng-minlength="5"/>

#script angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) { //$scope.text=''; $scope.change = function () {
                alert('change');
            }            
        });

初始化和不初始化text的条件下,change触发是不一样的哦,这里涉及到model初始化和更新机制。

ngCopy

适用标签:

  • a
  • input
  • select
  • textarea

官方api上说使用的标签是这些,我没明白a和select复制有啥子用。另外,我换个div实际上也能触发copy事件。一般常用的就是input和textarea。

触发条件:复制。鼠标右键复制和快捷键Ctrl+C都会触发。

代码语言:javascript
复制
#html 

	"LearnCtrl">"text" ng-copy="copy()"/>"30" rows="10" ng-copy="copy()">#script angular.module('learnModule', [])

        .controller('LearnCtrl', function ($scope) { $scope.copy = function () {
                alert('copy');
            }
        });

ngCut

适用标签:

  • a
  • input
  • select
  • textarea

触发条件:剪切。鼠标右键剪切和快捷键Ctrl+X都会触发。

代码语言:javascript
复制
#html "LearnCtrl">
    "text" ng-cut="cut()"/>
    "30"</span> rows=<span class="hljs-string">"10"</span> ng-cut=<span class="hljs-string">"cut()"</span>></div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.cut = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
                alert(<span class="hljs-string">'cut'</span>);
            }
        });</pre>
<h2 id="articleHeader7">ngPaste</h2><p><strong>适用标签:</strong> </p><ul><li><strong>a</strong> </li><li><strong>input</strong> </li><li><strong>select</strong> </li><li><strong>textarea</strong> </li></ul><p><strong>触发条件:粘贴。鼠标右键粘贴和快捷键Ctrl+V都会触发。</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <input type=<span class="hljs-string">"text"</span> ng-paste=<span class="hljs-string">"paste()"</span>/>
    <textarea cols=<span class="hljs-string">"30"</span> rows=<span class="hljs-string">"10"</span> ng-paste=<span class="hljs-string">"paste()"</span>></div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.paste = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
                alert(<span class="hljs-string">'paste'</span>);
            }
        });</pre>
<h2 id="articleHeader8">ngKeydown</h2><p><strong>适用标签:所有</strong> </p><p>个人感觉还是input和textarea比较常用</p><p><strong>触发条件:键盘按键按下</strong> </p><p>要把$event传过去,一般都是要判断按了哪个按键的。</p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <input type=<span class="hljs-string">"text"</span> ng-keydown=<span class="hljs-string">"keydown($event)"</span>/>
    <textarea cols=<span class="hljs-string">"30"</span> rows=<span class="hljs-string">"10"</span> ng-keydown=<span class="hljs-string">"keydown($event)"</span>></div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.keydown = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                alert(<span class="hljs-variable">$event</span>.keyCode);
            }
        });</pre>
<h2 id="articleHeader9">ngKeyup</h2><p><strong>适用标签:所有</strong> </p><p>个人感觉还是input和textarea比较常用</p><p><strong>触发条件:键盘按键按下并松开</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <input type=<span class="hljs-string">"text"</span> ng-keyup=<span class="hljs-string">"keyup($event)"</span>/>
    <textarea cols=<span class="hljs-string">"30"</span> rows=<span class="hljs-string">"10"</span> ng-keyup=<span class="hljs-string">"keyup($event)"</span>></div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.keyup = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                alert(<span class="hljs-variable">$event</span>.keyCode);
            }
        });</pre>
<h2 id="articleHeader10">ngKeypress</h2><p><strong>适用标签:所有</strong> </p><p>个人感觉还是input和textarea比较常用</p><p><strong>触发条件:键盘按键按下</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <input type=<span class="hljs-string">"text"</span> ng-keypress=<span class="hljs-string">"keypress($event)"</span>/>
    <textarea cols=<span class="hljs-string">"30"</span> rows=<span class="hljs-string">"10"</span> ng-keypress=<span class="hljs-string">"keypress($event)"</span>></div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.keypress = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                alert(<span class="hljs-variable">$event</span>.keyCode);
            }
        });</pre>
<h2 id="articleHeader11">keydown,keypress,keydown三者区别</h2><h3 id="articleHeader12">引发事件的按键</h3><p>非字符键不会引发 KeyPress 事件,但非字符键却可以引发 KeyDown 和 KeyUp 事件。</p><h3 id="articleHeader13">事件引发的时间</h3><p>KeyDown 和 KeyPress 事件在按下键时发生,KeyUp 事件在释放键时发生。</p><h3 id="articleHeader14">事件发生的顺序</h3><p>KeyDown -> KeyPress -> KeyUp。如果按一个键很久才松开,发生的事件为:KeyDown -> 
KeyPress -> KeyDown -> KeyPress -> KeyDown -> KeyPress ->
 ... -> KeyUp。</p><ul><li>KeyDown触发后,不一定触发KeyUp,当KeyDown 按下后,拖动鼠标,那么将不会触发KeyUp事件。</li><li>KeyPress主要用来捕获数字(注意:包括Shift+数字的符号)、字母(注意:包括大小写)、小键盘等除了F1-12、SHIFT、
Alt、Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单
键}、{开始键}和方向键外的ANSI字符。</li><li>KeyDown 和KeyUp 通常可以捕获键盘除了PrScrn所有按键(这里不讨论特殊键盘的特殊键)。</li><li>KeyPress 只能捕获单个字符。</li><li>KeyDown 和KeyUp 可以捕获组合键。</li><li>KeyPress 可以捕获单个字符的大小写。</li><li>KeyDown和KeyUp 对于单个字符捕获的KeyValue 都是一个值,也就是不能判断单个字符的大小写。</li><li>KeyPress 不区分小键盘和主键盘的数字字符。</li><li>KeyDown 和KeyUp 区分小键盘和主键盘的数字字符。</li><li>其中PrScrn 按键KeyPress、KeyDown和KeyUp 都不能捕获。</li></ul><h2 id="articleHeader15">ngMousedown</h2><p><strong>适用标签:所有</strong><br /> <strong>触发条件:鼠标按下,左右中间按下都会触发</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <button ng-mousedown=<span class="hljs-string">"mousedown($event)"</span>>button</div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.mousedown = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                alert(<span class="hljs-variable">$event</span>.which);
            }
        });</pre>
<h2 id="articleHeader16">ngMouseup</h2><p><strong>适用标签:所有</strong><br /> <strong>触发条件:鼠标按下弹起,左右中间按下弹起都会触发</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <button ng-mouseup=<span class="hljs-string">"mouseup($event)"</span>>button</div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.mouseup = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$event</span>)</span> </span>{
                alert(<span class="hljs-variable">$event</span>.which);
            }
        });</pre>
<h2 id="articleHeader17">ngMouseenter</h2><p><strong>适用标签:所有</strong><br /> <strong>触发条件:鼠标进入</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <button ng-mouseenter=<span class="hljs-string">"mouseenter()"</span>>button</div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.mouseenter = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
                alert(<span class="hljs-string">'mouseenter'</span>);
            }
        });</pre>
<h2 id="articleHeader18">ngMouseleave</h2><p><strong>适用标签:所有</strong><br /> <strong>触发条件:鼠标离开</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <button ng-mouseleave=<span class="hljs-string">"mouseleave()"</span>>button</div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.mouseleave = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
                alert(<span class="hljs-string">'mouseleave'</span>);
            }
        });</pre>
<h2 id="articleHeader19">ngMousemove</h2><p><strong>适用标签:所有</strong><br /> <strong>触发条件:鼠标移动</strong> </p>
<pre class="hljs php"><span class="hljs-comment">#html</span> <div ng-controller=<span class="hljs-string">"LearnCtrl"</span>>
    <button ng-mousemove=<span class="hljs-string">"mousemove()"</span>>button</div><span class="hljs-comment">#script</span> angular.module(<span class="hljs-string">'learnModule'</span>, [])

        .controller(<span class="hljs-string">'LearnCtrl'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(<span class="hljs-variable">$scope</span>)</span> </span>{ <span class="hljs-variable">$scope</span>.mousemove = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">()</span> </span>{
                alert(<span class="hljs-string">'mousemove'</span>);
            }
        });</pre>
</div>
</pre><!--END .entry-content -->
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-09-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ngClick
  • ngDblclick
  • ngBlur
  • ngFocus
  • ngChange
  • ngCopy
  • ngCut
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档