Angular不能改变其他指令的ng-model形式

时间:2016-03-28 08:48:12

标签: angularjs directive angular-directive

我需要通过单击pdfViewer指令中的canvas来更改pdfViewerToolbar指令中的ng-model(currentPage)。当我点击画布时调用控制器中的self.next函数,但在pdfViewerToolbar指令中没有更改ng-model =“currentPage”。

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 个答案:

答案 0 :(得分:2)

您需要将currentPage变量作为隔离指令传递,如下所示。

 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: "="
    },
    ...
    ...
    ...

现在从pdfViewer指令模板传递那个currentPage,如下所示:

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);
    };

答案 1 :(得分:1)

在你的pdfViewerToolbar指令中scope是孤立的,如果你想在该指令中改变某些东西,你可以将它作为范围元素传递给双向数据绑定:"="

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

并使用您的指令传递控制器模型,如此

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