【问题标题】:AngularJS Material mdDialog and unhandled rejection on a promiseAngularJS Material mdDialog 和未处理的承诺拒绝
【发布时间】:2017-08-17 23:05:35
【问题描述】:

我不知道为什么,但我在隐藏/取消时收到 unhandled rejection AngularJS 材料 mdDialog 错误

mdDialog 的代码:

$scope.addAttendee = function(ev) {
    $mdDialog.show({
    controller: DialogController,
    templateUrl: 'views/regForm.tmpl.html',
    parent: angular.element(document.body),
    targetEvent: ev,
    clickOutsideToClose:true,
    controllerAs: 'ctrl',
    fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
    locals: {parent: $scope}
})
.then(function(response){
    if(angular.isDefined(response)){
        attendees.push(response);
    }

    console.log(attendees);
    console.log(attendees.length);
})

当我删除一个承诺时,对话框可以关闭而没有错误消息。

有什么线索吗?

【问题讨论】:

  • '未处理的拒绝'意味着一个承诺被拒绝但错误没有得到处理。添加.catch 以找出错误所在,并在每次有可能拒绝承诺时执行此操作。
  • 尝试像这样添加拒绝功能:.then(function(response){}, function(reject){ //console.log(reject)}) 来解决错误。
  • 我看到 mdDialog 仍然让你有些头疼,哈哈。问候。老兄@JackTheKnife
  • $mdDialog.cancel() 返回一个被拒绝的承诺是正常的
  • 如果$mgDialog.hide() 收到未处理的拒绝,则很可能.then 块中的代码引发了错误。可能变量attendees 不是数组,因此Uncaught TypeError: Cannot read property 'push'。在.then 块之后添加.catch 块以查看错误。

标签: angularjs angularjs-material


【解决方案1】:

mdDiaglog 的承诺没有更改(隐藏/取消)时,我需要一个函数。

更新后的代码

$scope.addAttendee = function(ev) {
    $mdDialog.show({
    controller: DialogController,
    templateUrl: 'views/regForm.tmpl.html',
    parent: angular.element(document.body),
    targetEvent: ev,
    clickOutsideToClose:true,
    controllerAs: 'ctrl',
    fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
    locals: {parent: $scope}
})
.then(
    function(response){
        if(angular.isDefined(response)){
            attendees.push(response);

            console.log(attendees);
            console.log(attendees.length);
        }
    },
    function(){
        //no changes
    }
)
.catch(
    function(error) {
        console.error('Error: ' + error);
    }
);  

【讨论】:

  • 或任何人:您将如何加载/显示一个 mdDialog 框以在 catch 调用中显示错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 2023-03-17
  • 2018-04-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多