私は ui-router で AngularJS を使用しています。https://github.com/sayanee/angularjs-pdfで入手できる AngularJS PDF ディレクティブを利用しようとしています。すぐにPDFへのURLがないため、指定された例にいくつかの変更を加える必要があります。
しかし、私は問題に遭遇しました。PDF に関連付けられた id 値をルートに渡すため、ルートの変更が成功するまで、そのパラメーターの解決を待つ必要があります。これは、 https://github.com/angular-ui/ui-router/wikiの [解決] セクションのガイダンスに従って達成しました。
しかし、ページにアクセスすると、これらのいずれかが解決される前に、ファイルの URL を解決するためにこの ID を使用して API にクエリを実行する前に、ディレクティブをクリックしていることに気付きます。もちろん、ルートが解決の約束をまだ返していないため、ディレクティブのスコープに値を入力していません。したがって、これは null 値であり、最終的に pdf.js で URL が「ない」というエラーが発生します。 t が設定されています。
必要に応じてスコープが適切に設定されるまで、ディレクティブ内のロジックの実行を遅らせるにはどうすればよいですか?
したがって、上部にリンクしたディレクティブをダウンロードする必要はありません。私が取り組んでいるものは次のとおりです。
(function () {
'use strict';
angular.module('pdf', []).directive('ngPdf', function ($window) {
return {
restrict: 'E',
templateUrl: function (element, attr) {
return attr.templateUrl ? attr.templateUrl : 'viewer.html';
},
link: function (scope, element, attrs) {
var url = scope.pdfUrl,
pdfDoc = null,
pageNum = 1,
scale = (attrs.scale ? attrs.scale : 1),
canvas = (attrs.canvasid ? document.getElementById(attrs.canvasid) : document.getElementById('pdf-canvas')),
ctx = canvas.getContext('2d'),
windowEl = angular.element($window);
windowEl.on('scroll', function () {
scope.$apply(function () {
scope.scroll = windowEl[0].scrollY;
});
});
PDFJS.disableWorker = true;
scope.pageNum = pageNum;
scope.renderPage = function (num) {
pdfDoc.getPage(num).then(function (page) {
var viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
};
scope.goPrevious = function () {
if (scope.pageNum <= 1)
return;
scope.pageNum = parseInt(scope.pageNum, 10) - 1;
};
scope.goNext = function () {
if (scope.pageNum >= pdfDoc.numPages)
return;
scope.pageNum = parseInt(scope.pageNum, 10) + 1;
};
scope.zoomIn = function () {
scale = parseFloat(scale) + 0.2;
scope.renderPage(scope.pageNum);
return scale;
};
scope.zoomOut = function () {
scale = parseFloat(scale) - 0.2;
scope.renderPage(scope.pageNum);
return scale;
};
scope.changePage = function () {
scope.renderPage(scope.pageNum);
};
scope.rotate = function () {
if (canvas.getAttribute('class') === 'rotate0') {
canvas.setAttribute('class', 'rotate90');
} else if (canvas.getAttribute('class') === 'rotate90') {
canvas.setAttribute('class', 'rotate180');
} else if (canvas.getAttribute('class') === 'rotate180') {
canvas.setAttribute('class', 'rotate270');
} else {
canvas.setAttribute('class', 'rotate0');
}
};
PDFJS.getDocument(url).then(function (_pdfDoc) {
pdfDoc = _pdfDoc;
scope.renderPage(scope.pageNum);
scope.$apply(function () {
scope.pageCount = _pdfDoc.numPages;
});
});
scope.$watch('pageNum', function (newVal) {
if (pdfDoc !== null)
scope.renderPage(newVal);
});
}
};
});
})();
特に必要だとは思いませんが、コントローラーの簡略図を次に示します。
angular.module('myApp').controller('myController', function($scope, $http, $state) {
var url = "http://example.com/myService";
$http.get(url)
.success(function(data, status, headers, config) {
$scope.pdfUrl = data.url;
$scope.pdfName = data.name;
$scope.scroll = 0;
$scope.getNavStyle = function(scroll) {
if (scroll > 100) return 'pdf-controls fixed';
else return 'pdf-controls';
}
})
.error(function(data, status, headers, config) {
$state.go('failure.state');
});
});
ui ビューは次のようになります。
<ng-pdf template-url="pathToTemplate/viewer.html" canvasid="pdf" scale="1.5"></ng-pdf>
ありがとう!