scope.modal.show()不是函数?

时间:2015-08-22 11:44:48

标签: javascript angularjs ionic-framework ionic

我尝试打开模态视图。为此,我使用以下内容:

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

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

  ionic.extend(scope, options);

  scope.modal = $ionicModal.fromTemplate(template, {
    scope: scope
  });

  // Show modal and initialize cropper.
  return scope.modal.show().then(function() {
    return (new jrCropController(scope)).promise.promise;
  });
},

这很好用。但现在我不想在javascript中定义我的模板。我想在一个额外的html文件中定义我的模板,所以我做了。

所以我不得不替换它:

$ionicModal.fromTemplate(template,

用这个:

$ionicModal.fromTemplateUrl(url,

但现在我收到以下错误:

ionic.bundle.js:20306 TypeError: scope.modal.show is not a function
at Object.crop (jr-crop.js:287)
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)

为什么在使用模板网址而不是模板时会出现此错误?对我来说没有意义,因为在两种情况下它都是相同的方法。

2 个答案:

答案 0 :(得分:5)

我认为您错过了function showButton(){ $("#test").show(); } 返回模态对象而fromTemplate()返回承诺的事实,因为您正在异步加载模板。

因此,这很好用

fromTemplateUrl()

虽然这会工作

scope.modal = $ionicModal.fromTemplate(template, {
    scope: scope
});

因为scope.modal = $ionicModal.fromTemplateUrl(url, { scope: scope }); 将是一个承诺对象。

您必须等待承诺解决然后分配模式,例如

scope.modal

供参考,请查看$ionicModal: fromTemplate vs. fromTemplateUrl functions

答案 1 :(得分:2)

$ionicModal.fromTemplateUrl(url, {
    scope: $scope,
}).then(function(modal) {
    $scope.modal = modal;
});

Iconic modal