AngularJS学习--- 事件处理(Event Handlers) ng-click操作 step 10
2014-05-15 00:42
447 查看
本文主要通过介绍ng-click方法来对angularjs中的事件处理方法做个了解.
3.代码实现
查看step-9和step-10之间的代码差异:https://github.com/angular/angular-phonecat/compare/step-9...step-10
Controllers(控制器)
注:控制器这里定义了一个setImage方法,就是将mainImageUrl的值设置为当前imageUrl.
CSS(样式)
app/css/app.css
改变鼠标移动上去的样式为指针形.
Template(模板)
app/partials/phone-detail.html
这里定义了一个ng-click方法,这里将当前的img作为参数传过去,然后,setImage方法将mainImageUrl的值替换为当前点击的图片,从而实现点击小图片,左边的图片被放大.
执行如下命令进行测试:
同时在phone-detail.html中加入:
效果如下图所示:
这样就完成了一个ng-click的操作.
事件处理方法,除了ng-click还有其它如:
ngMousemove
ngMouseleave
ngMouseover
ngKeyup
ngSubmit
....
可以根据个人需要进行选择,和JS本身自带的方法差不多,这里只不过angularjs又多封装了一层.
1.切换目录
git checkout step-10 npm start
2.效果
点击右边的小图片,那么左边框中将显示大图片,示例如下:3.代码实现
查看step-9和step-10之间的代码差异:https://github.com/angular/angular-phonecat/compare/step-9...step-10
Controllers(控制器)
app/js/controllers.js:
'use strict'; /* Controllers */ var phonecatControllers = angular.module('phonecatControllers', []); phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) { $http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); $scope.orderProp = 'age'; }]); phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) { $http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) { $scope.phone = data; $scope.mainImageUrl = data.images[0]; }); $scope.setImage = function(imageUrl) { $scope.mainImageUrl = imageUrl; } }]);
注:控制器这里定义了一个setImage方法,就是将mainImageUrl的值设置为当前imageUrl.
CSS(样式)
app/css/app.css
ul.phone-thumbs img:hover { cursor: pointer; }
改变鼠标移动上去的样式为指针形.
Template(模板)
app/partials/phone-detail.html
<img ng-src="{{mainImageUrl}}" class="phone"> ... <ul class="phone-thumbs"> <li ng-repeat="img in phone.images"> <img ng-src="{{img}}" ng-click="setImage(img)"> </li> </ul> ...
这里定义了一个ng-click方法,这里将当前的img作为参数传过去,然后,setImage方法将mainImageUrl的值替换为当前点击的图片,从而实现点击小图片,左边的图片被放大.
4.测试:
test/e2e/scenarios.js
... describe('Phone detail view', function() { ... it('should display the first phone image as the main phone image', function() { expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/); }); it('should swap main image if a thumbnail image is clicked on', function() { element(by.css('.phone-thumbs li:nth-child(3) img')).click(); expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.2.jpg/); element(by.css('.phone-thumbs li:nth-child(1) img')).click(); expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/); }); });
执行如下命令进行测试:
npm run protractor #测试结果如下: ------------------------------------ PID: 4250 (capability: chrome #1) ------------------------------------ Using ChromeDriver directly... ....... Finished in 9.867 seconds 7 tests, 11 assertions, 0 failures
5.实验:
在Controllers中的PhoneDetailCtrl加入:$scope.hello = function(name) { alert('Hello ' + (name || 'world') + '!'); }
同时在phone-detail.html中加入:
<h1>{{phone.name}}</h1> <button id="{{phone.name}}" ng-click="hello(phone.name)">Hello</button> <p>{{phone.description}}</p> ...
效果如下图所示:
这样就完成了一个ng-click的操作.
事件处理方法,除了ng-click还有其它如:
ngMousemove
ngMouseleave
ngMouseover
ngKeyup
ngSubmit
....
可以根据个人需要进行选择,和JS本身自带的方法差不多,这里只不过angularjs又多封装了一层.
相关文章推荐
- 事件处理(Event Handlers) ng-click操作
- AngularJS学习--- 动画操作 (Applying Animations) ngAnimate step 12
- AngularJS(九)ng-click(事件),angular.module 函数,表单,输入验证
- Angular使用操作事件指令ng-click传多个参数示例
- [Q学习]10 Qt事件系统——处理事件
- angular-ng-click用于操作事件的指令(传多个参数)
- 一步步学习SPD2010--第三章节--处理列表和库(10)----添加自定义操作
- JQuery学习笔记(六)——css操作、事件处理和动画
- angularJS 单击ng-click和双击ng-dblclick嵌套或者出现在同一元素 避免事件互相干扰方法
- angularjs html事件(ng-click,ng-hide,ng-show)
- AngularJS学习--- AngularJS中模板链接和图像 ng-src step6
- 一步步学习SPD2010--第三章节--处理列表和库(10)----添加自定义操作
- AngularJs学习记录-(2) ng-click
- [学习]触摸屏拖拽事件处理
- JavaScript学习-事件处理
- 关于FLEX事件堆积 导致处理操作叠加 问题
- 学习Microsoft .Net Remoting系列专题(Remoting事件处理全接触)
- Silverlight学习笔记一:DataGrid如何处理鼠标的滚轮事件
- 窗口关闭 事件处理 -Java学习笔记(36)
- SilverLight学习笔记--Silverligh之在Silverlight中捕获和处理DOM事件