[英]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.