简体   繁体   English

如何将结果从POST请求传递到Ionic和Angular中的另一个页面

[英]How to pass results from POST request to another page in Ionic and Angular

I'm using ionic to build a mobile app and I'm implementing very lightweight authentication (no security) into the app. 我正在使用ionic来构建移动应用程序,并且正在对该应用程序实施非常轻量级的身份验证(无安全性)。 Basically when a users hits the login button after submitting their email and password, their information is checked against a database using a POST request. 基本上,当用户在提交电子邮件和密码后单击登录按钮时,将使用POST请求针对数据库检查其信息。 Now my question is, once I have confirmed that the user's information is in the database, I'd like to pass that response object from the POST to a profile page that can then show the user's profile information. 现在我的问题是,一旦我确认用户信息已在数据库中,我想将该响应对象从POST传递到一个配置文件页面,然后可以显示用户的配置文件信息。 How can I pass information (the response object) from one controller to the another page so it can then be displayed on screen? 如何将信息(响应对象)从一个控制器传递到另一个页面,以便随后将其显示在屏幕上? Code below: 代码如下:

app.js app.js

    //Login view
     .state('signin', {
      url: '/signin',
      templateUrl: 'templates/signin.html',
      controller: 'LoginCtrl'
  })

  // Profile view:
  .state('tab.profile', {
    url: '/profile',
    views: {
      'tab-profile': {
        templateUrl: 'templates/tab-profile.html'
        controller: 'ProfileCtrl'
      }
    }
  })

controllers.js: controllers.js:

$http.post(url, obj)
         .success(function (res, status, headers, config) {
          if (res == null){
             console.log("bad login");
          }
          else{
           // $scope.response = res;
            console.log(res);
            console.log("post successful");
            $state.go('tab.profile',{response:res});
          }
         });

tab-profile.html 制表profile.html

<ion-view view-title="Profile">
  <ion-content>
    <ion-list>
      <ion-item >{{response.name}}</ion-item>
    </ion-list>
  </ion-content>
</ion-view>

You can create a service which will store data to be passed to other controllers . 您可以创建一个service ,该service将存储要传递给其他controllers

Let me show an example: 让我举一个例子:

var yourApp = angular.module('fooApp', []);

yourApp.factory('yourFactory', function() {
    var items = [];
    var storeService = {};

    storeService.addItem = function(item) {
        items.push(item);
    };
    storeService.removeItem = function(item) {
        var index = items.indexOf(item);
        items.splice(index, 1);
    };
    storeService.items = function() {
        return items;
    };

    return storeService;
});

function MyCtrl($scope, yourFactory) {
    $scope.newItem = {};
    $scope.yourFactory = yourFactory;    
}

You can define params in the definiton of a route. 您可以在路线的定义中定义参数。 Look at the docu ( https://github.com/angular-ui/ui-router/wiki/URL-Routing ) of the ui-router module to see the right syntax. 查看ui-router模块的文档( https://github.com/angular-ui/ui-router/wiki/URL-Routing )以查看正确的语法。

In order to pass more complex object between two controllers i would use a service. 为了在两个控制器之间传递更复杂的对象,我将使用服务。 Xou can set the data in the service after the POST is resolved and after the state change read it in the other controller. 解决POST后,状态更改后,Xou可以在服务中设置数据,然后在另一个控制器中读取它。 Read this link to see examples Passing data between controllers in Angular JS? 阅读此链接以查看示例在Angular JS中的控制器之间传递数据吗? . Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将POST请求的结果存储在Ionic和Angular中的另一个控制器中使用 - How to store result from POST request to use in another controller in Ionic and Angular Angular Ionic 4 如何将变量从 page.html 传递到 page.ts - Angular Ionic 4 How to pass a Variable from page.html to page.ts Ionic + Angular - POST请求后如何避免“404 Not Found(from cache)”? - Ionic + Angular - How to avoid the “404 Not Found (from cache)” after POST request? Angular - 如何从一个页面链接到另一个页面并在不使用id的情况下滚动到离子中的锚点 - Angular - how to link from one page to another page and scroll to an anchor in ionic without using id ionic 2 / Angular 2 - 无法将数据从Google Maps InfoWindow传递到页面 - ionic 2/ Angular 2 - Fail to pass data from Google Maps InfoWindow to page Express Node.js-如何将request.files传递到另一个POST请求 - Express Nodejs - How to pass request.files to another POST request Ionic + Angular POST请求返回状态404 - Ionic + Angular POST request return state 404 离子/角 HTTP 发布请求不适用于 android - Ionic/Angular HTTP post request not working on android Angular + Ionic Post请求未找到404 - Angular + Ionic Post request getting 404 not found 如何以角度在单页应用程序中将数据从一页传递到另一页 - How to pass data from one page to another in single page application in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM