💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
这个原文为:Event Handlers。 具体什么意思,我们不管了。看看想达到的效果吧。 http://guide.mengyunzhi.com/angular-phonecat/app/#!/phones/motorola-atrix-4g ![](https://box.kancloud.cn/2016-08-04_57a30c4509bd5.png) 点击右侧的图片,左侧显示图片的大图。示例中的动画效果,我们将放在第十四章实现,本节仅实现点击后,显示相应的大图。 ## 重构页面 使用bootstrap的栅格系统,我们首先对页面进行重构,让它看起来更接近我们的示例站点。 ![](https://box.kancloud.cn/2016-08-04_57a30c4533284.png) `yun-zhi/phone-detail.template.html` ~~~ <div class="container-fluid"> <div class="row"> <div class="col-md-4"></div> <div class="col-md-8"></div> </div> <div class="row"> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> <div class="col-md-2"></div> </div> </div> ~~~ 重新整理元素 ~~~ <div class="container-fluid"> <div class="row"> <div class="col-md-4"> <img ng-src="{{$ctrl.phone.images[0]}}" class="phone img-responsive img-rounded" /> </div> <div class="col-md-8"> <h1>{{$ctrl.phone.name}}</h1> <p>{{$ctrl.phone.description}}</p> <div class="row"> <div class="col-md-2" ng-repeat="img in $ctrl.phone.images"> <img ng-src="{{img}}" class="img-thumbnail" /> </div> </div> </div> </div> <div class="row"> <div class="col-md-2"> <h4>Availability and Networks</h4> <dl> <dt>Availability</dt> <dd ng-repeat="availability in $ctrl.phone.availability">{{availability}}</dd> </dl> </div> <div class="col-md-2"> <h4>Availability and Networks</h4> <dl> <dt>Availability</dt> <dd ng-repeat="availability in $ctrl.phone.availability">{{availability}}</dd> </dl> </div> <div class="col-md-2"> <h4>Battery</h4> <dl> <dt>Type</dt> <dd>{{$ctrl.phone.battery.type}}</dd> <dt>Talk Time</dt> <dd>{{$ctrl.phone.battery.talkTime}}</dd> <dt>Standby time (max)</dt> <dd>{{$ctrl.phone.battery.standbyTime}}</dd> </dl> </div> <div class="col-md-2"> <h4>Storage and Memory</h4> <dl> <dt>RAM</dt> <dd>{{$ctrl.phone.storage.ram}}</dd> <dt>Internal Storage</dt> <dd>{{$ctrl.phone.storage.flash}}</dd> </dl> </div> <div class="col-md-2"> <h4>Connectivity</h4> <dl> <dt>Network Support</dt> <dd>{{$ctrl.phone.connectivity.cell}}</dd> <dt>WiFi</dt> <dd>{{$ctrl.phone.connectivity.wifi}}</dd> <dt>Bluetooth</dt> <dd>{{$ctrl.phone.connectivity.bluetooth}}</dd> <dt>Infrared</dt> <dd ng-bind-html="$ctrl.phone.connectivity.infrared | checkmark"></dd> <dt>GPS</dt> <dd ng-bind-html="$ctrl.phone.connectivity.gps | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Android</h4> <dl> <dt>OS Version</dt> <dd>{{$ctrl.phone.android.os}}</dd> <dt>UI</dt> <dd>{{$ctrl.phone.android.ui}}</dd> </dl> </div> <div class="col-md-2"> <h4>Size and Weight</h4> <dl> <dt>Dimensions</dt> <dd ng-repeat="dim in $ctrl.phone.sizeAndWeight.dimensions">{{dim}}</dd> <dt>Weight</dt> <dd>{{$ctrl.phone.sizeAndWeight.weight}}</dd> </dl> </div> <div class="col-md-2"> <h4>Display</h4> <dl> <dt>Screen size</dt> <dd>{{$ctrl.phone.display.screenSize}}</dd> <dt>Screen resolution</dt> <dd>{{$ctrl.phone.display.screenResolution}}</dd> <dt>Touch screen</dt> <dd ng-bind-html="$ctrl.phone.display.touchScreen | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Hardware</h4> <dl> <dt>CPU</dt> <dd>{{$ctrl.phone.hardware.cpu}}</dd> <dt>USB</dt> <dd>{{$ctrl.phone.hardware.usb}}</dd> <dt>Audio / headphone jack</dt> <dd>{{$ctrl.phone.hardware.audioJack}}</dd> <dt>FM Radio</dt> <dd ng-bind-html="$ctrl.phone.hardware.fmRadio | checkmark"></dd> <dt>Accelerometer</dt> <dd ng-bind-html="$ctrl.phone.hardware.accelerometer | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Camera</h4> <dl> <dt>Primary</dt> <dd>{{$ctrl.phone.camera.primary}}</dd> <dt>Features</dt> <dd>{{$ctrl.phone.camera.features.join(', ')}}</dd> </dl> </div> <div class="col-md-2"> <h4>Additional Features</h4> <dd>{{$ctrl.phone.additionalFeatures}}</dd> </div> </div> </div> ~~~ 最终效果: ![](https://box.kancloud.cn/2016-08-04_57a30c454905f.png) ## 添加点击事件 如果以前我们接触过JS,相信对on-click肯定不陌生。在angularjs中,我们使用ng-click来绑定点击事件。 `yun-zhi/phone-detail.component.js` ~~~ + // 设置大图 + self.setImage = function setImage(imgUrl){ + console.log(imgUrl); + }; ~~~ `yun-zhi/phone-detail.template.html` ~~~ <div class="col-md-2" ng-repeat="img in $ctrl.phone.images"> - <img ng-src="{{img}}" class="img-thumbnail" /> + <img ng-click="$ctrl.setImage(img)" ng-src="{{img}}" class="img-thumbnail" /> </div> ~~~ 测试: 点击图片后,已经将图片的地址传入C层。 ![](https://box.kancloud.cn/2016-08-04_57a30c456f901.png) ## 绑定数据 下面,我们将传入值绑定到左侧的大图上。 `yun-zhi/phone-detail.component.js` ~~~ // 设置大图 self.setImage = function setImage(imgUrl){ - console.log(imgUrl); + self.mainImageUrl = imgUrl; }; ... self.phone = response.data; + + // 将大图设置为第一张图片 + self.setImage(self.phone.images[0]); }); ~~~ `yun-zhi/phone-detail.template.html` ~~~ <div class="col-md-4"> - <img ng-src="{{$ctrl.phone.images[0]}}" class="phone img-responsive img-rounded" /> + <img ng-src="{{$ctrl.mainImageUrl}}" class="phone img-responsive img-rounded" /> </div> ~~~ 总结: 点用户点击图片时,能过setImage()将图片的地址传给C层,C层再通过数据双向绑定对左侧大图的URL地址进行了替换。从而达到了点击右侧缩略图显示左侧大图的目的。 * * * * * `yun-zhi/phone-detail.template.html` ~~~ <div class="container-fluid"> <div class="row"> <div class="col-md-4"> <img ng-src="{{$ctrl.mainImageUrl}}" class="phone img-responsive img-rounded" /> </div> <div class="col-md-8"> <h1>{{$ctrl.phone.name}}</h1> <p>{{$ctrl.phone.description}}</p> <div class="row"> <div class="col-md-2" ng-repeat="img in $ctrl.phone.images"> <img ng-click="$ctrl.setImage(img)" ng-src="{{img}}" class="img-thumbnail" /> </div> </div> </div> </div> <div class="row"> <div class="col-md-2"> <h4>Availability and Networks</h4> <dl> <dt>Availability</dt> <dd ng-repeat="availability in $ctrl.phone.availability">{{availability}}</dd> </dl> </div> <div class="col-md-2"> <h4>Availability and Networks</h4> <dl> <dt>Availability</dt> <dd ng-repeat="availability in $ctrl.phone.availability">{{availability}}</dd> </dl> </div> <div class="col-md-2"> <h4>Battery</h4> <dl> <dt>Type</dt> <dd>{{$ctrl.phone.battery.type}}</dd> <dt>Talk Time</dt> <dd>{{$ctrl.phone.battery.talkTime}}</dd> <dt>Standby time (max)</dt> <dd>{{$ctrl.phone.battery.standbyTime}}</dd> </dl> </div> <div class="col-md-2"> <h4>Storage and Memory</h4> <dl> <dt>RAM</dt> <dd>{{$ctrl.phone.storage.ram}}</dd> <dt>Internal Storage</dt> <dd>{{$ctrl.phone.storage.flash}}</dd> </dl> </div> <div class="col-md-2"> <h4>Connectivity</h4> <dl> <dt>Network Support</dt> <dd>{{$ctrl.phone.connectivity.cell}}</dd> <dt>WiFi</dt> <dd>{{$ctrl.phone.connectivity.wifi}}</dd> <dt>Bluetooth</dt> <dd>{{$ctrl.phone.connectivity.bluetooth}}</dd> <dt>Infrared</dt> <dd ng-bind-html="$ctrl.phone.connectivity.infrared | checkmark"></dd> <dt>GPS</dt> <dd ng-bind-html="$ctrl.phone.connectivity.gps | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Android</h4> <dl> <dt>OS Version</dt> <dd>{{$ctrl.phone.android.os}}</dd> <dt>UI</dt> <dd>{{$ctrl.phone.android.ui}}</dd> </dl> </div> <div class="col-md-2"> <h4>Size and Weight</h4> <dl> <dt>Dimensions</dt> <dd ng-repeat="dim in $ctrl.phone.sizeAndWeight.dimensions">{{dim}}</dd> <dt>Weight</dt> <dd>{{$ctrl.phone.sizeAndWeight.weight}}</dd> </dl> </div> <div class="col-md-2"> <h4>Display</h4> <dl> <dt>Screen size</dt> <dd>{{$ctrl.phone.display.screenSize}}</dd> <dt>Screen resolution</dt> <dd>{{$ctrl.phone.display.screenResolution}}</dd> <dt>Touch screen</dt> <dd ng-bind-html="$ctrl.phone.display.touchScreen | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Hardware</h4> <dl> <dt>CPU</dt> <dd>{{$ctrl.phone.hardware.cpu}}</dd> <dt>USB</dt> <dd>{{$ctrl.phone.hardware.usb}}</dd> <dt>Audio / headphone jack</dt> <dd>{{$ctrl.phone.hardware.audioJack}}</dd> <dt>FM Radio</dt> <dd ng-bind-html="$ctrl.phone.hardware.fmRadio | checkmark"></dd> <dt>Accelerometer</dt> <dd ng-bind-html="$ctrl.phone.hardware.accelerometer | checkmark"></dd> </dl> </div> <div class="col-md-2"> <h4>Camera</h4> <dl> <dt>Primary</dt> <dd>{{$ctrl.phone.camera.primary}}</dd> <dt>Features</dt> <dd>{{$ctrl.phone.camera.features.join(', ')}}</dd> </dl> </div> <div class="col-md-2"> <h4>Additional Features</h4> <dd>{{$ctrl.phone.additionalFeatures}}</dd> </div> </div> </div> ~~~ `yun-zhi/phone-detail.component.js` ~~~ angular. module('yunZhi'). component('phoneDetail', { templateUrl: 'yun-zhi/phone-detail.template.html', controller: ['$http', '$routeParams', function PhoneListController($http, $routeParams) { var self = this; // 设置大图 self.setImage = function setImage(imgUrl){ self.mainImageUrl = imgUrl; }; // 使用$routeParam获取phoneId // $routeParam存在于angular-route模块中 var phoneId = $routeParams.phoneId; // 拼接存放手机数据的文件地址 var url = 'phones/' + phoneId + '.json'; // 发起请求 $http.get( url ). then(function(response){ console.log(response); self.phone = response.data; // 将大图设置为第一张图片 self.setImage(self.phone.images[0]); }); } ] }); ~~~