从ngDialog控制器中调用ngDialog不起作用

时间:2016-08-18 10:10:23

标签: angularjs ng-dialog

我有一个ngDialog模式,显示上传的图像并允许用户发表评论。这就是我所说的:

$scope.openShowPic = function (imageName, imagedescription, username, imagelikeslength, comments, imageid) {
$scope.imageName = imageName;
        $scope.imageDescription = imagedescription;
        $scope.username = username;
        $scope.imagelikeslength = imagelikeslength;
        $scope.comments = comments;
        $scope.imageid = imageid;

        ngDialog.open({ template: 'views/showpic.html', scope: $scope, 
            className: 'ngdialog-theme-mine dialogwidth800', controller:"ShowPicController" });
    };

如果用户的令牌在提交评论之前到期,我会有一个错误处理程序,它会检测401错误状态,然后确保用户已注销并关闭showPic ngDialog。然后打开登录ngDialog,以便用户登录。

$scope.submitComment = function () {

        CommentFactory.save({id: $scope.imageid}, $scope.mycomment)
        .$promise.then(
              function (response) {
                console.log(response);
              },
              function (error) {
                console.log('error');

                if (error.status == 401){

                   AuthFactory.logout();
                   $scope.loggedIn = false;
                   $scope.username = '';
                   ngDialog.close();

                    ngDialog.open({ template: 'views/login.html', scope: $scope, 
                    className: 'ngdialog-theme-default', controller:"LoginController" });
                  }
              }); 

        $scope.commentForm.$setPristine();

        $scope.mycomment = {
            comment: ""
        };

    }; 

不幸的是,当用户点击登录时,登录ngDialog没有与LoginController通信,因为没有任何反应。调用登录ngDialog的代码与我在标题中使用的代码完全相同,并且工作正常。 显然我已经破坏了一些但看不到的东西! 谢谢。

1 个答案:

答案 0 :(得分:0)

好的,我尝试从promise.then的错误部分内部登录ngDialog,但无济于事。最后我发现解决这个问题的最好方法是在error函数中设置一个布尔值,然后根据布尔值的结果打开登录ngDialog。

$scope.submitComment = function () {
        $scope.needLogin = false;
        CommentFactory.save({id: $scope.imageid}, $scope.mycomment)
        .$promise.then(
              function (response) {
                console.log(response);
              },
              function (error) {
                console.log('error');

                if (error.status == 401){

                   AuthFactory.logout();
                   $scope.loggedIn = false;
                   $scope.username = '';
                   $scope.needLogin = true;
                  }
              }); 

        $scope.commentForm.$setPristine();

        $scope.mycomment = {
            comment: ""
        };
        if ($scope.needLogin == true) {
            ngDialog.open({ template: 'views/login.html', scope: $scope, 
                className: 'ngdialog-theme-default', controller:"LoginController" });
        };      
    };           
}])  
相关问题