搜尋

首頁  >  問答  >  主體

angular.js - 請教angular中scope作用域的問題

AppendBookApp.controller('MainController', ['$rootScope', function($rootScope) {
    
    $rootScope.image = {};
    $rootScope.image.exists = true;
    $rootScope.image.src = '';

    $rootScope.uploader = WebUploader.create({
        server: '/upload/test',
        swf: '/libs/WebUploader/Uploader.swf',
        chunked: true,
        disableGlobalDnd: true,
        dnd: document.body,
        paste: document.body,
        pick: {
            id: '#UploadImageButton',
            label: '点击选择图片'
        },
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/*'
        },
        compress: false,
        auto: true
    });

    // setTimeout(function() {
    //     $rootScope.image.src = '';
    //     console.log($rootScope.image.src);
    // }, 2000);
    //     $rootScope.image.src = '/uploadfile/2c12c2170d0a2eaee8c061060f9f91f1.png';

    $rootScope.uploader.onUploadSuccess = function(file, res) {
        $rootScope.image.src = res._raw;
        $rootScope.image.exists = true;
        console.log($rootScope.image.src);
    };

    $('.webuploader-pick').removeClass('webuploader-pick').addClass('btn btn-primary');

}]);

為什麼我在上傳成功的回呼函數裡修改scope,介面沒反應

伊谢尔伦伊谢尔伦2744 天前464

全部回覆(1)我來回復

  • 伊谢尔伦

    伊谢尔伦2017-05-15 17:00:56

    其實資料已經修改成功了,介面沒有反應是因為沒有觸發digest,程式碼改成下面就好了

    $rootScope.uploader.onUploadSuccess = function(file, res) {
        $rootScope.$apply(function () {
            $rootScope.image.src = res._raw;
            $rootScope.image.exists = true;
            console.log($rootScope.image.src);
        });
    };

    回覆
    0
  • 取消回覆