2016-08-18 65 views
0

我有一個ngDialog模式,它顯示上傳的圖像並允許用戶發表評論。這是我怎麼稱呼它:從ngDialog控制器內調用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的代碼與我在標題中使用的完全相同,並且工作正常。 顯然我打破了一些東西,但看不到什麼! 謝謝。

回答

0

好吧我試圖從promise.then的錯誤部分獲取登錄ngDialog工作,但無濟於事。最後我發現解決這個問題的最好方法是在錯誤函數中設置一個布爾值,然後根據布爾值的結果打開登錄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" }); 
     };  
    };   
}])