开发者问题收集

Angular TypeError 无法读取未定义的属性‘then’

2015-08-22
1577

我有一个名为 crop 的函数

$scope.crop = function (id) {
    var options = {
        template: "templates/polaroids_edit_crop.html",
        url: PolaroidsService.getPolaroid(id).polaroid,
        width: 300,
        height: 300
    };

    $jrCrop.crop(options).then(function (canvas) {
        PolaroidsService.setCropped(id, canvas.toDataURL());
    });
};

但是当我调用此函数时,我在此行收到错误:

$jrCrop.crop(options).then(function (canvas)

其中包含:

TypeError: Cannot read property 'then' of undefined
at Scope.$scope.crop (polaroids_edit.js:51)
at $parseFunctionCall (ionic.bundle.js:21044)
at ionic.bundle.js:53458
at Scope.$eval (ionic.bundle.js:23100)
at Scope.$apply (ionic.bundle.js:23199)
at HTMLButtonElement.<anonymous> (ionic.bundle.js:53457)
at HTMLButtonElement.eventHandler (ionic.bundle.js:11713)
at triggerMouseEvent (ionic.bundle.js:2863)
at tapClick (ionic.bundle.js:2852)
at HTMLDocument.tapTouchEnd (ionic.bundle.js:2975)

$jrCrop.crop(options) 执行以下操作:

crop: function (options) {
    options = this.initOptions(options);

    var scope = $rootScope.$new(true);

    ionic.extend(scope, options);

    $ionicModal.fromTemplateUrl(template, {
        scope: scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        scope.modal = modal;

        return scope.modal.show().then(function () {
            return (new jrCropController(scope)).promise.promise;
        });
    });
}

我必须等待模板加载,并且只有加载后我才能返回模式。我该如何解决这个问题?

1个回答

我认为您应该返回 $ionicModal.fromTemplateUrl 结果:

crop: function(options) {
    options = this.initOptions(options);

    var scope = $rootScope.$new(true);

    ionic.extend(scope, options);

    return $ionicModal.fromTemplateUrl(template, {
        scope: scope,
        animation: 'slide-in-up'
    }).then(function(modal) {
        scope.modal = modal;
        return scope.modal.show().then(function() {
            return (new jrCropController(scope)).promise.promise;
        });
    });
}
dfsq
2015-08-22