通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例
更新時(shí)間:2017年01月03日 15:14:31 作者:程序大大
本篇文章主要介紹了通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例,廢話不多說了,具體如下:
從項(xiàng)目中截出的代碼
HTML部分:
<section> <img src="image/user-tuijian/tuijian_banner.png" /> <div> <form ng-submit="submit_form()"> <input type="text" name="aaa" placeholder="商品名稱:" ng-model="form.goods_name" /> <input type="text" name="bbb" placeholder="商品網(wǎng)址:" ng-model="form.goods_url" /> <textarea placeholder="您寶貴的留言就是我們前進(jìn)的動(dòng)力!" ng-model="form.user_msg"></textarea> <div> <div ng-repeat="item in thumb"> <!-- 采用angular循環(huán)的方式,對存入thumb的圖片進(jìn)行展示 --> <label> <img ng-src="{{item.imgSrc}}"/> </label> <span ng-if="item.imgSrc" ng-click="img_del($index)"></span> </div> <div ng-repeat="item in thumb_default"> <!-- 這里之所以寫個(gè)循環(huán),是為了后期萬一需要多個(gè)‘加號'框 --> <label> <input type="file" id="one-input" accept="image/*" file-model="images" onchange="angular.element(this).scope().img_upload(this.files)"/> </label> </div> </div> <p>(*^_^*)請?jiān)敿?xì)描述您的需求,有助于我們快速定位并解決問題,希望我們的產(chǎn)品和服務(wù)能得到您的肯定。</p> <input type="submit" name="" value="提 交" /> </form> </div> </section>
JS部分:
Module.controller('controlName', ['$scope', '$http', function($scope, $http) { $scope.reader = new FileReader(); //創(chuàng)建一個(gè)FileReader接口 $scope.form = { //用于綁定提交內(nèi)容,圖片或其他數(shù)據(jù) image:{}, }; $scope.thumb = {}; //用于存放圖片的base64 $scope.thumb_default = { //用于循環(huán)默認(rèn)的‘加號'添加圖片的框 1111:{} }; $scope.img_upload = function(files) { //單次提交圖片的函數(shù) $scope.guid = (new Date()).valueOf(); //通過時(shí)間戳創(chuàng)建一個(gè)隨機(jī)數(shù),作為鍵名使用 $scope.reader.readAsDataURL(files[0]); //FileReader的方法,把圖片轉(zhuǎn)成base64 $scope.reader.onload = function(ev) { $scope.$apply(function(){ $scope.thumb[$scope.guid] = { imgSrc : ev.target.result, //接收base64 } }); }; var data = new FormData(); //以下為像后臺(tái)提交圖片數(shù)據(jù) data.append('image', files[0]); data.append('guid',$scope.guid); $http({ method: 'post', url: '/comm/test-upload.php?action=success', data:data, headers: {'Content-Type': undefined}, transformRequest: angular.identity }).success(function(data) { if (data.result_code == 'SUCCESS') { $scope.form.image[data.guid] = data.result_value; $scope.thumb[data.guid].status = 'SUCCESS'; console.log($scope.form) } if(data.result_code == 'FAIL'){ console.log(data) } }) }; $scope.img_del = function(key) { //刪除,刪除的時(shí)候thumb和form里面的圖片數(shù)據(jù)都要?jiǎng)h除,避免提交不必要的 var guidArr = []; for(var p in $scope.thumb){ guidArr.push(p); } delete $scope.thumb[guidArr[key]]; delete $scope.form.image[guidArr[key]]; }; $scope.submit_form = function(){ //圖片選擇完畢后的提交,這個(gè)提交并沒有提交前面的圖片數(shù)據(jù),只是提交用戶操作完畢后, 到底要上傳哪些,通過提交鍵名或者鏈接,后臺(tái)來判斷最終用戶的選擇,整個(gè)思路也是如此 $http({ method: 'post', url: '/comm/test.php', data:$scope.form, }).success(function(data) { console.log(data); }) }; }]);
最后的效果如圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 每個(gè)Angular版本在其生命周期中都經(jīng)歷了各個(gè)階段。組件在Angular中起著關(guān)鍵作用; 在這里,本文將討論Angular中的組件生命周期以及它們?nèi)绾斡绊懣蚣芩邪姹镜纳芷凇?/div> 2021-05-05
Angular 與 Component store實(shí)踐示例
這篇文章主要為大家介紹了Angular 與 Component store實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02AngularJs ng-change事件/指令的用法小結(jié)
本篇文章主要介紹了AngularJs ng-change事件/指令的小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11AngularJS入門教程之AngularJS表達(dá)式
AngularJS應(yīng)用表達(dá)式是純javascript表達(dá)式,并輸出它們被使用的數(shù)據(jù)在那里。本文給大家介紹AngularJS入門教程之AngularJS表達(dá)式,對angularjs表達(dá)式相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-04-04詳細(xì)解讀AngularJS中的表單驗(yàn)證編程
這篇文章主要介紹了詳細(xì)解讀AngularJS中的表單驗(yàn)證編程,AngularJS是一個(gè)非常熱門的JavaScript庫,需要的朋友可以參考下2015-06-06最新評論