【问题标题】:How to instantiate a canvas in a dialog?如何在对话框中实例化画布?
【发布时间】:2016-12-19 11:44:36
【问题描述】:

我想实例化一个对话框中可用的画布,但它总是返回一个空对象。在我的 JavaScript 文件中,我有:

     $scope.showAdvanced = function(ev) {
        var useFullScreen = ($mdMedia('sm') || $mdMedia('xs'))  && $scope.customFullscreen;
        var myDialog = $mdDialog.show({
            controller: DialogController,
            templateUrl: '../templates/dialogCanvas.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose:true,
            fullscreen: useFullScreen,
        })
                .then(function(answer) {
                    $scope.status = 'You said the information was "' + answer + '".';
        }, function() {
            $scope.status = 'You cancelled the dialog.';
        });
        $scope.$watch(function() {
            return $mdMedia('xs') || $mdMedia('sm');
        }, function(wantsFullScreen) {
            $scope.customFullscreen = (wantsFullScreen === true);
        });
    };

在我的 HTML 文件 (dialogCanvas.html) 中,我有这个标签:

<div id="back">
    <canvas id="canvasZone"></canvas>
</div>

最后在控制器函数中,我尝试通过这样做来实例化画布:

    function DialogController($scope, $mdDialog) {
        var canvas = document.getElementById('canvasZone');
    }

但是对象是空的。

【问题讨论】:

    标签: angularjs dialog material-design


    【解决方案1】:

    尝试使用$timeout$evalAsync获取canvas对象。这样里面的函数会在DOM操作后执行。

    $scope.$evalAsync(function(){
        var canvas = document.getElementById('canvasZone');
    });
    

    $timeout(function(){
        var canvas = document.getElementById('canvasZone');
    },0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多