簡體   English   中英

嘗試從angularjs范圍打開GET頁面

[英]attempting to open a GET page from an angularjs scope

我正在嘗試調用get方法以在angularjs范圍內打開頁面。 get方法在spring控制器中定義。 get方法稱為成功,但是頁面從不加載。 以下是代碼段

.then(function successCallback(response) {
                    if (response.status == 204) {

                    } else if (response.status == 200) {
                        $scope.nredit = {

                        };
                        $scope.goToView.goToView(); //call the get method here
                    }

這是調用GET方法的作用域,但頁面永遠不會打開

$scope.goToView = {
                goToView: function () {
                    $http({
                        method: 'GET',
                        url: '/mypage',
                    }).then(function successCallback(response) {
                        if (response.status == 400) {
                            alert('Not allowed to view this page!');
                        } else if (response.status == 200) {
                           // $scope.users = response.data;
                        }
                    }, function errorCallback(response) {
                        alert(JSON.stringify(response));
                    });
                }

            }

這是在spring中定義的get方法控制器

@RequestMapping(value = "/mypage", method = RequestMethod.GET)
    public ModelAndView goToView(ModelAndView model, HttpServletRequest request, HttpSession userSession) throws IOException {  
        model.setViewName("view");
        return model;
    }

為什么從角度調用時視圖無法打開/加載

從AngularJS文檔中:

$ http服務是AngularJS的核心服務,可促進通過瀏覽器的XMLHttpRequest對象或JSONP與遠程HTTP服務器的通信。

簡而言之:使用$ http,您正在執行AJAX請求。 因此,這將在response.data返回目標頁面(/ mypage)的內容。 這不會觸發重定向。

您應該使用AngularJS路由。 您可以在本教程中找到一個示例: https : //www.w3schools.com/angular/angular_routing.asp

您可以使goToView可用於作用域...在其中包含作用域的作用域對象有點過頭了。 您應該兌現承諾,以提高可讀性。

handleResponse替換成功處理程序形式的初始諾言,您沒有顯示響應,所以要告訴發生了什么。

let getPage = () => {
     return $http({
       method: 'GET',
       url: '/mypage',
    });
}

let goToPage = (response) => {
    if (response.status == 400) {
         alert('Not allowed to view this page!');
    } else if (response.status == 200) {
         $scope.users = response.data;
    }
};

let handleResponse = (response) {
    ... dowhater
   return $scope.goToView();
}

$scope.goToView = () => {
   return getPage()
      .then(goToPage)
}

我只是再次閱讀了問題-您不應該以這種方式請求頁面。 我認為它在做頁面上的某件事。 這行不通。 您應該使用路由,或者如果使用ui-router,則可以使用狀態樹。

$state.go('mystate')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM