ng-click

AngularJS入门(用ng-model指令实现双向绑定)

断了今生、忘了曾经 提交于 2020-04-22 04:02:11
上一章介绍了AngularJS框架的插值语法和ng-bind指令,本章介绍ng-model指令,本指令用于实现input和变量的双向绑定。 示例代码: <!DOCTYPE html> <html ng-app> <head> <meta charset="utf-8"> <title>ng-model directive</title> </head> <body ng-controller="HelloController"> <div> <p>双向绑定</p> <input ng-model="greeting"> <p>Hello {{greeting || "World"}}</p> <button ng-click="init()">重置</button> <hr> </div> <script src="../lib/angularjs/1.2.26/angular.min.js"></script> <script> function HelloController($scope) { $scope.init = function() { $scope.greeting = "Hello"; } } </script> </body> </html> input加上ng-model指令后,框架会负责input和greeting变量的自动同步。 插值中支持表达式语法

angularJS如何处理事件冒泡

浪尽此生 提交于 2019-12-09 06:50:06
事件冒泡和事件捕捉一直以来都是被讨论的话题,也许大家平时在工作中没有遇到过需要解决事件冒泡的情况 举个栗子: <body ng-click="fun1()"> <div ng-click="fun2()"> <img ng-click="fun3()" src="xxx.png"/> </div> </body> 从以上的代码中的fun1(),fun2()和fun3()我们可以看出,当我们点击了<img/>标签中的ng-click事件,触发fun3()方法, 但是根据文档对象模型的特征,我们虽然只想触发fun3()方法,事件一直向上一层进行冒泡,fun2()和fun1()也会随后执行 此时,为了达到只执行fun3()方法的效果,我们要要在fun3()中写入组织事件冒泡的代码 在这里着重讲一下angularJs中是如何实现阻止事件冒泡的 当我们在一个标签上使用了controller中写好的方法时 <div fun1($event)></div> angularJS中的执行方法会自带一个$event,这个$event是当前事件的对象,我们直接对这个事件对象进行操作就可以达到阻止事件冒泡的效果 $scope.fun1=function($event){ $event.stopPropagation(); //stopPropagation是目前最常用也是最标准的解决事件冒泡的方法 /

深入了解angularjs中的$digest与$apply方法,从区别聊到使用优化

对着背影说爱祢 提交于 2019-12-07 17:28:32
壹 ❀ 引 如果有人问,在angularjs中修改模型数据为何视图会同步更新呢,我想大多数人一定会回答 脏检查(Dirty Checking) 相关概念。没错, 在angularjs中作用域(scope)作为链接控制器(controller)与视图(view)之间的桥梁 ,除了绑定数据监听事件外,一旦有数据发生改变,scope还兼顾了脏检测更新视图的职责,这是我们宏观的理解。 这就引发了一系列的问题,以点击事件为例,为什么在angularjs中用原生click事件达不到更新视图的效果?ng-click与原生click有何区别?ng-click触发后angularjs又是怎么让视图更新的呢?$digest和$apply这两个眼熟的方法究竟有何作用,两者有什么区别?如果你对于这些问题感兴趣,不妨静下心来读一读本文,那么本文开始。 贰 ❀ angularjs的数据绑定 现在有个需求,当我们点击按钮时需要更新视图中的文本信息,当然不通过angularjs我们使用原生js做法也能轻易实现,像这样: <div>我的名字是:<span class="name"></span></div> <button class="btn">click me</button> let btn = document.querySelector('.btn'); let name = document

AngularJS - how to use ng-click without submitting the form?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a form that has both ng-click and ng-submit. ng-submit is meant for the submission, while ng-click calls a separate function like upload, etc. How do I make sure that ng-click does not accidentally submits the form? thank you! 回答1: ngClick does not submit the form. Sometimes, you can have problems because you have two button elements in your form , and they both submit it. To avoid that, specify the type "button" on it. Example : function demo ( $scope ) { $scope . doSubmit = function () { alert ( 'I submit' ); }; $scope .

Why does ng-click not work in case-1, but does in case-3?

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: There is clearly something fundamental im not yet understanding. Im trying to make user of the Modal module in Angular Ui.Bootstrap but im finding that my clicks are not activating the open() function -- So boiling it down to a very simple testcase, as below, im not seeing any calls when the ng-click points to a function (alert or console.log), but does work when the ng-click points to something which is just an expression Why is the alert not called in the first example? <div data-ng-app > <button data-ng-click = "alert('Message 1

Toggle class with ng-click on several elements

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I toggle classes on several elements individually with ng-click? In this question https://stackoverflow.com/a/22072110/2169327 toggling classes with a click was done like this: CSS: .red { color: red; } JS: $scope.toggle = false; HTML: Change Class But what if I have several buttons that each should toggle its own class with ng-click? If I set it up in this way: HTML: Change Class Change Class Both buttons get toggled if I press one. I know a workaround is to define an own ng-click event for each button (f.ex toggle1 for button1,

angularjs ng-click silently eats errors

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: If I have an ng-click like this: ng-click="buggy()" and click on no error message is generated on the console. This makes it a bit tricky to debug. Why aren't error messages generated? Anything I can do? 回答1: Angular expressions Actually, It's not something special with ng-click , It's the default behavior of angular expressions . buggy() is not evaluated with regular javascript. It is evaluated with $parse . $parse evaluates expressions and returns a function that would run against a scope. $parse only log errors when the

搜索

被刻印的时光 ゝ 提交于 2019-11-29 17:34:26
<div class= "box-tools pull-right" > <div class= "has-feedback" > 品牌名称 :<input type= "text" ng-model= "searchEntity.name" > 品牌首字母 :<input type= "text" ng-model= "searchEntity.firstChar" > <input class= "btn btn-default" ng-click= "reloadList()" type= "button" value= " 查询 " > </div> </div> 来源: https://www.cnblogs.com/lijun6/p/11524941.html