pdf文件预览指令
1.指令定义
app.directive('embedPdf', function() {
return {
restrict: 'E',
scope: {
attachment: '=attachment'
},
template:'',
link: function(scope, element, attrs) {
function showAttachment(attachment) {
if (!attachment) {
element.html('<div align="center">文件不存在。</div>');
}else if(!(attachment.mimeType == 'application/pdf')){
element.html('<div align="center"><h3>文件不是PDF格式文件,无法预览。</h3></div>');
}else if(attachment.mimeType == 'application/pdf'){
element.html('<embed width="850px" height="495px" src="' + app.contextPath + '/attachments/file/' + attachment.name + '" type="application/pdf"></embed>');
}
}
scope.$watch('attachment', function(nv, ov) {
showAttachment(nv);
});
showAttachment(scope.attachment);
}
};
});
2.js代码
$scope.view = function(id){
jddoclstRes.findAttach({'attId':id},{},function(r){
$scope.attachment = r;
});
$scope.viewDlg.visible = true;
}
3.指令使用
<embed-pdf attachment="attachment"/>
4.命名规则
<embed-pdf/>~~对应'embedPdf'