您好,登錄后才能下訂單哦!
這篇文章主要介紹angularjs怎么實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
具體內(nèi)容如下
directive.js
/* * 多圖片上傳及預(yù)覽指令(需指定圖片類名) * */ angular.module('routerModule').directive('fileModel', ['$parse', 'fileReader', function($parse, fileReader) { return { restrict:'A', link:function(scope, element, attrs, ngModel) { var model = $parse(attrs.fileModel); var modelSetter = model.assign; var imgviewID = attrs["imgViewId"]; var imgView = angular.element(document.querySelector("."+imgviewID)); element.bind('change', function(event) { scope.$apply(function() { modelSetter(scope, element[0].files[0]); }); //附件預(yù)覽 scope.file = (event.srcElement || event.target).files[0]; fileReader.readAsDataUrl(scope.file, scope).then(function(result) { imgView.attr("src",result); }); }); } } }]); angular.module('routerModule').factory('fileReader', ["$q", "$log", function($q, $log) { var onLoad = function(reader, deferred, scope) { return function() { scope.$apply(function() { deferred.resolve(reader.result); }); } } var onError = function(reader, deferred, scope) { return function() { scope.$apply(function() { deferred.reject(reader.result); }); }; }; var getReader = function(deferred, scope) { var reader = new FileReader(); reader.onload = onLoad(reader, deferred, scope); reader.onerror = onError(reader, deferred, scope); return reader; }; var readAsDataURL = function(file, scope) { var deferred = $q.defer(); var reader = getReader(deferred, scope); reader.readAsDataURL(file); return deferred.promise; } return { readAsDataUrl: readAsDataURL }; } ]);
html關(guān)鍵代碼
<section class="infogroup"> <h5>法人證件</h5> <div class="row relatedCard"> <div class="col-md-4 txtcenter"> <p>身份證正面</p> <div><img ng-src="showImg/{{dealer.idCardFace}}" class="idCardFace" enlarge-pic/></div> <span class="btn btn-file btn btn-xs btn-primary btn-editable picEdit"><i class="fa fa-edit"></i> 點(diǎn)擊編輯 <input type="file" file-model="idCardFace" img-view-id="idCardFace" /> </span> </div> <div class="col-md-4 txtcenter"> <p>身份證反面</p> <div><img ng-src="showImg/{{dealer.idCardBack}}" class="idCardBack" enlarge-pic/></div> <span class="btn btn-file btn btn-xs btn-primary btn-editable picEdit"><i class="fa fa-edit"></i> 點(diǎn)擊編輯 <input type="file" file-model="idCardBack" img-view-id="idCardBack" /> </span> </div> </div> </section>
controller.js中發(fā)送到后臺(tái)的數(shù)據(jù)
$scope.postData = { dealerId: $scope.dealer.dealerId, companyName: $scope.dealer.companyName, companySize: $scope.dealer.companySize, idCardFace: angular.element(document.querySelector(".idCardFace"))[0].src, idCardBack: angular.element(document.querySelector(".idCardBack"))[0].src }; $http.post('updateDealerCertificate',$scope.postData).success(function(data){ <!-- 成功執(zhí)行代碼 --> });
預(yù)覽如下:
以上是“angularjs怎么實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。