2016-03-28 61 views
0

我需要通過單擊pdfViewer指令中的畫布來更改pdfViewerToolbar指令中的ng模型(currentPage)。控制器中的self.next函數在我點擊畫布時被調用,但是ng-model =「currentPage」在pdfViewerToolbar指令中沒有改變。Angular不能更改其他指令的ng模型

app.angular.module("pdf").directive("pdfViewerToolbar", ["pdfDelegate", function(e) { 
 
    return { 
 
     restrict: "E", 
 
     template: '<div class="pdf_navigation" ng-show="navBar">\ 
 
      <div class="pdf_buttons">\ 
 
       <div ng-click="prev()" class="goPrevious" title="previous page"></div>\ 
 
       <div ng-click="next()" class="goNext" id="goNext" title="next page"></div>\ 
 
       <div ng-click="zoomOut()" class="zoomIn"></div>\ 
 
       <div ng-click="zoomIn()" class="zoomOut"></div>\ 
 
      </div>\ 
 
      <div class="pdf_page">\ 
 
       <span>Page</span>\ 
 
       <input type="text" min=1 ng-model="currentPage" maxlength="4" ng-change="goToPage()" >\ 
 
       <span>of {{pageCount}}</span>\ 
 
      </div>\ 
 
     </div>', 
 
     scope: { 
 
      pageCount: "=", 
 
      navBar: "=", 
 
     }, 
 
     // controller: "PdfCtrl", 
 
     link: function(t, n, a) { 
 
      var o = a.delegateHandle; 
 
      t.currentPage = 1, t.prev = function() { 
 
       e.$getByHandle(o).prev(), r() 
 
      }, t.next = function() { 
 
       e.$getByHandle(o).next(), r() 
 
      }, t.zoomIn = function() { 
 
       e.$getByHandle(o).zoomIn() 
 
      }, t.zoomOut = function() { 
 
       e.$getByHandle(o).zoomOut() 
 
      }, t.rotate = function() { 
 
       e.$getByHandle(o).rotate() 
 
      }, t.goToPage = function() { 
 
       e.$getByHandle(o).goToPage(t.currentPage) 
 
      }; 
 
      var r = function() { 
 
       t.currentPage = e.$getByHandle(o).getCurrentPage() 
 
      } 
 

 

 
     } 
 
    } 
 
}]) 
 

 
app.angular.module("pdf").directive("pdfViewer", ["pdfDelegate", function(r) { 
 
    return { 
 
     restrict: "E", 
 
     template: '<div show-control class="pdf_doc"><pdf-viewer-toolbar ng-if="showToolbar" delegate-handle="{{id}}" page-count="pageCount" nav-bar="pdfNavigationBar"></pdf-viewer-toolbar><canvas ng-click="next()" id="pdf-canvas" ></canvas></div>', 
 
     scope: false, 
 
     controller: "PdfCtrl", 
 
     link: function(e, t, n) { 
 
      e.id = n.delegateHandle, e.showToolbar = e.$eval(n.showToolbar) || !1 
 

 
      var o = n.delegateHandle; 
 
      e.currentPage = 1, 
 
      e.next = function() { 
 
       r.$getByHandle(o).next(), s() 
 
      } 
 

 
      var s = function() { 
 
       e.currentPage = r.$getByHandle(o).getCurrentPage() 
 
      } 
 

 

 
     } 
 

 

 
    } 
 
}]); 
 

 

 
app.controller('PdfCtrl', [ 
 
    '$scope', 
 
    '$element', 
 
    '$attrs', 
 
    'pdfDelegate', 
 
    '$log', 
 
    '$q', '$rootScope', 
 
    function($scope, $element, $attrs, pdfDelegate, $log, $q, $rootScope) { 
 

 
     // Register the instance! 
 
     var deregisterInstance = pdfDelegate._registerInstance(this, $attrs.delegateHandle); 
 
     // De-Register on destory! 
 
     $scope.$on('$destroy', deregisterInstance); 
 

 
     var self = this; 
 

 
     var url = $scope.$eval($attrs.url); 
 
     var headers = $scope.$eval($attrs.headers); 
 
     var pdfDoc; 
 
     $scope.pageCount = 0; 
 
     var currentPage = 1; 
 
     var angle = 0; 
 
     var scale = $attrs.scale ? $attrs.scale : 1; 
 
     var canvas = $element.find('canvas')[0]; 
 
     var ctx = canvas.getContext('2d'); 
 

 
     self.next = function() { 
 
      if (currentPage >= pdfDoc.numPages) 
 
       return; 
 
      currentPage = parseInt(currentPage, 10) + 1; 
 

 
      renderPage(currentPage); 
 

 
      console.log('currentPage'+currentPage); 
 
     }; 
 

 
      return PDFJS 
 
       .getDocument(docInitParams) 
 
       .then(function (_pdfDoc) { 
 
        console.log('loaded'); 
 
        $rootScope.loadPdf = $scope.pdfNavigationBar = true; 
 

 
        pdfDoc = _pdfDoc; 
 
        renderPage(1); 
 
        $scope.$apply(function() { 
 
         $scope.pageCount = _pdfDoc.numPages; 
 
        }); 
 

 
       }, function(error) { 
 
        $log.error(error); 
 
        return $q.reject(error); 
 
       }) 
 
     }; 
 

 
     if(url) self.load(); 
 
    }]);

回答

2

您需要將當前頁變量傳遞像下面分離指令。

return { 
    restrict: "E", 
    template: '<div class="pdf_navigation" ng-show="navBar">\ 
     <div class="pdf_buttons">\ 
      <div ng-click="prev()" class="goPrevious" title="previous page"></div>\ 
      <div ng-click="next()" class="goNext" id="goNext" title="next page"></div>\ 
      <div ng-click="zoomOut()" class="zoomIn"></div>\ 
      <div ng-click="zoomIn()" class="zoomOut"></div>\ 
     </div>\ 
     <div class="pdf_page">\ 
      <span>Page</span>\ 
      <input type="text" min=1 ng-model="currentPage" maxlength="4" ng-change="goToPage()" >\ 
      <span>of {{pageCount}}</span>\ 
     </div>\ 
    </div>', 
    scope: { 
     pageCount: "=", 
     navBar: "=", 
     currentPage: "=" 
    }, 
    ... 
    ... 
    ... 

,現在通過從PDF查看器指令模板當前頁象下面這樣:

app.angular.module("pdf").directive("pdfViewer", ["pdfDelegate", function(r) { 
    return { 
    restrict: "E", 
    template: '<div show-control class="pdf_doc"><pdf-viewer-toolbar ng-if="showToolbar" delegate-handle="{{id}}" page-count="pageCount" nav-bar="pdfNavigationBar" current-page="currentPage" ></pdf-viewer-toolbar><canvas ng-click="next()" id="pdf-canvas" ></canvas></div>', 
    scope: false, 
    controller: "PdfCtrl", 

,現在在從同一控制器PdfCtrl和訪問範圍定義變量。

app.controller('PdfCtrl', [ 
'$scope', 
'$element', 
'$attrs', 
'pdfDelegate', 
'$log', 
'$q', '$rootScope', 
function($scope, $element, $attrs, pdfDelegate, $log, $q, $rootScope) { 

    // Register the instance! 
    var deregisterInstance = pdfDelegate._registerInstance(this, $attrs.delegateHandle); 
    // De-Register on destory! 
    $scope.$on('$destroy', deregisterInstance); 

    var self = this; 

    var url = $scope.$eval($attrs.url); 
    var headers = $scope.$eval($attrs.headers); 
    var pdfDoc; 
    $scope.pageCount = 0; 
    $scope.currentPage = 1; 
    var angle = 0; 
    var scale = $attrs.scale ? $attrs.scale : 1; 
    var canvas = $element.find('canvas')[0]; 
    var ctx = canvas.getContext('2d'); 

    self.next = function() { 
     if ($scope.currentPage >= pdfDoc.numPages) 
      return; 
     $scope.currentPage = parseInt($scope.currentPage, 10) + 1; 

     renderPage($scope.currentPage); 

     console.log('currentPage'+$scope.currentPage); 
    }; 
+0

驚人的,謝謝! – johanson

1

在你pdfViewerToolbar指令scope是孤立的,如果你想改變在該指令東西,你通過前人的精力它作爲雙向綁定範圍元素:"="

scope: { 
     pageCount: "=", 
     navBar: "=", 
     currentPage:"=" 
    } 

,並使用您的指令,通過控制器模型這樣

<pdf-viewer-toolbar page-count="ctrlModelPageCount" nav-bar="ctrlModelNavBar" current-page="ctrlModelCurrentPage"></pdf-viewer-toolbar>