为什么范围的位置。$ apply()在这种情况下很重要

时间:2016-03-11 11:59:24

标签: javascript angularjs

我已经修复了一个问题,但我不明白我想要的东西。

在这个问题中,我问为什么dom没有在更新时更新,并且向我建议我使用范围。$ apply()这样做: Angular not updating Ng-class when on-change is triggered

我的问题是为什么当我放置范围。$ apply()在我的函数结束时使我的代码工作但是当我把它放在reader.onload函数或在scope.slideMenuToggle()函数之后它不起作用?

    /**
     * Triggers reader for when an image has been selected from the input
     * @param element - Saves the input value to this scope.
     */

    scope.setImageFile = function (element) {
        var reader = new FileReader();

        // Converts the image to a data URL.
        reader.readAsDataURL(element.files[0]);
        scope.slideMenuToggle();

        /**
         * When chosen image is selected it triggers the onload function to pass the image to the whiteboardService.
         * @param event - Saves the event of the input field to get the images data URL.
         */
        reader.onload = function (event) {
            fabric.Image.fromURL(event.target.result, function (img) {
                whiteboardService.uploadImageToCanvas(img);
            });

            // Resets the form that wraps round the file input to allow the
            // user to add more than one of the same file.
            // NOTE: This will break other inputs if you put them inside this form
            $('.app-set-image-file-form').trigger('reset');
        };

        scope.$apply();
    };

TA

0 个答案:

没有答案