2016-04-24 5 views
0

角度の新しいjs(1週間前)です。私は、リクエストを送信するために$ httpを使用していたという要件があります。私は、別のページの応答メッセージで受け取ったエラーを表示したいのですが、別のページに値を渡す方法を理解できません。新しいページは、http calls.Belowを作成しているものとは異なります。私はこのコードを書いています。何も動作していないようです。この要件についてどうやって進むのか教えてください。エラーメッセージJSONを別のページテンプレートに渡します。

$http({ 
      method : 'POST', 
      url  : 'http://localhost:8080/abc/users', 
      data  : JSON.stringify(toPass) 
     }).then(function (data) { 
      // this callback will be called asynchronously 
      // when the response is available 
      console.log(data.data+'a'); 
      $scope.result="The request has been successfully submitted" +data.data.emailId; 
      $scope.x=data.data.emailId; 
      console.log('result'+x); 
      console.log('result'); 
      //$location.path('/Success?{error1}'); 
      window.location="/Success?{x}"; 
      /*if (data.data.error){ 
       $scope.error=data.data.error 
      }else{ 
       $scope.result=data.data.emailId; 
      }*/ 
      }, function (error) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
      console.log("An error encountered"+error); 

      var error1=error.status; 

     // $location.path('/Success?{error1}'); 

      }); 
    } 
}) 

答えて

0

この

app.service(
      "friendService", 
      function($http, $q) { 
       // Return public API. 
       return({ 
        addFriend: addFriend, 
        getFriends: getFriends, 
        removeFriend: removeFriend 
       }); 
       // --- 
       // PUBLIC METHODS. 
       // --- 
       // I add a friend with the given name to the remote collection. 
       function addFriend(name) { 
        var request = $http({ 
         method: "post", 
         url: "api/index.cfm", 
         params: { 
          action: "add" 
         }, 
         data: { 
          name: name 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // I get all of the friends in the remote collection. 
       function getFriends() { 
        var request = $http({ 
         method: "get", 
         url: "api/index.cfm", 
         params: { 
          action: "get" 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // I remove the friend with the given ID from the remote collection. 
       function removeFriend(id) { 
        var request = $http({ 
         method: "delete", 
         url: "api/index.cfm", 
         params: { 
          action: "delete" 
         }, 
         data: { 
          id: id 
         } 
        }); 
        return(request.then(handleSuccess, handleError)); 
       } 
       // --- 
       // PRIVATE METHODS. 
       // --- 
       // I transform the error response, unwrapping the application dta from 
       // the API response payload. 
       function handleError(response) { 
        // The API response from the server should be returned in a 
        // nomralized format. However, if the request was not handled by the 
        // server (or what not handles properly - ex. server error), then we 
        // may have to normalize it on our end, as best we can. 
        if (
         ! angular.isObject(response.data) || 
         ! response.data.message 
         ) { 
         return($q.reject("An unknown error occurred.")); 
        } 
        // Otherwise, use expected error message. 
        return($q.reject(response.data.message)); 
       } 
       // I transform the successful response, unwrapping the application data 
       // from the API response payload. 
       function handleSuccess(response) { 
        return(response.data); 
       } 
      } 
     ); 
+0

のようなサービスを作るあなたの助けWasiqいただきありがとうございます。私はベースとしてあなたのコードを使用し、サービスを作成しました。 – Batman

0

コントローラを介してデータを共有するには、データを保存するサービスを使用して、共有の親コントローラまたは(より優れた)コントローラでデータを共有できます。サービスはAngularJsのシングルトンであり、これに適しています。

関連する問題