Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular modal instance can't read DOM elements

Hi im playing with angular modal instance..

But when i declare a querySelector in the modal controler it gives console error of Cannot read property 'querySelector' of null

HTML

      <body>

            <div id="signature-pad">

                    <canvas width="300" height="300"></canvas>

                    <div class="description">Sign above</div>
                    <button class="button clear" data-action="clear">Clear</button>

            </div>



</body>

    <div class="modal-footer">
        <button class="btn btn-success btn-lg" ng-click="ok();">Gem & Godkend</button>
        <button class="btn btn-warning btn-lg" ng-click="cancel()">Annuler</button>
   </div>

JS - ModalInstanceController

var ModalInstanceCtrl = function ($scope, $modalInstance) {


            $scope.init = function() {
                var wrapper = document.getElementById("signature-pad"),
                    clearButton = wrapper.querySelector("[data-action=clear]"),
                    canvas = wrapper.querySelector("canvas"),
                    signaturePad;

                signaturePad = new SignaturePad(canvas);

                clearButton.addEventListener("click", function (event) {
                    signaturePad.clear();
                });
                $scope.ok = function () {
                    $modalInstance.close(signaturePad.toDataURL());
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };


            };

        $modalInstance.opened.then($scope.init);

        };

So it seams like it, can't find dom elements in model .. :s As you i can i tried to load, model into dom first with $modalInstance.opened.then($scope.init); But this dosen't work either

like image 494
Stweet Avatar asked Nov 28 '25 12:11

Stweet


1 Answers

Umm seams that DOM elements needed to be loaded first.. found my answer here https://github.com/angular-ui/bootstrap/issues/2586

    $scope.init = function() {
var wrapper = document.getElementById("signature-pad"),
...
}
$modalInstance.opened.then($scope.init);

this initialize the dom elemtens first, so query selector can see the dom elements ! :)

i just need'ed <div ng-init="init()"> in html

like image 178
Stweet Avatar answered Nov 30 '25 00:11

Stweet