$mdDialog.prompt 抛出异常,但函数在 confirm 下工作正常

$mdDialog.prompt throws exception, but the function works fine with confirm

出于某种原因,我的提示对话框在我的 angular 全栈应用程序中停止工作。

我在谷歌上搜索了一个解决方案,告诉我更新我的 angular,我照做了,但没有解决问题。

$scope.showPrompt = function(ev, ret, value) {
  var confirm = $mdDialog.prompt()
  .title('Rediger ' + value)
  .textContent('Indtast en ny værdi for: ' + value)
  .placeholder('getValue()')
  .ariaLabel('Ny ' + value)
  .targetEvent(ev)
  .ok('Accepter')
  .cancel('Annuller');
$mdDialog.show(confirm).then(function(result) {
  //setValue(result);
});
};

每当我调用该函数时,我都会收到一条错误消息,提示类型错误:$mdDialog.prompt 不是一个函数。

如果我将对话框更改为 .confirm 并删除占位符,它就可以正常工作

编辑: 在 javascript 中,变量作为函数在运行时定义:

//This code throw an error
getValue();
var getValue = function(){};

你必须在调用它之前声明你的变量函数:

//Ok
var getValue = function(){};
getValue();

你也可以这样写

//Ok because code block is parsed before runtime
getValue();
function getValue(){};

所以这里你的 plunker 编辑了 https://plnkr.co/edit/YqeyaLqW2B6xn4VHcVlQ?p=preview

我发现一个答案已经被接受,但它没有解决我的问题所以我想提供一个替代建议。

我 运行 遇到了完全相同的情况,结果发现我使用的是 Angular Material 的旧版本,它还没有引入 .prompt() 。如果您查看 docs for the version I was using (1.0.5) you'll see in the demo that there is no prompt option for dialog. But if you look at the docs for the latest version(撰写本文时为 1.1.0),提示选项就在那里。

希望这对像我一样遇到这个问题的人有所帮助。