Angularjs 离子2在返回之前检查是否可见
我是离子型和棱角型的新手,来自多年的.NET开发。我正在网上尝试几个例子,用Ionic 2构建登录原型 我让WebAPI在后台工作,只返回JSON true或false,这取决于传递的凭据是否正确 我得到的身份验证提供程序如下所示:Angularjs 离子2在返回之前检查是否可见,angularjs,ionic-framework,ionic2,Angularjs,Ionic Framework,Ionic2,我是离子型和棱角型的新手,来自多年的.NET开发。我正在网上尝试几个例子,用Ionic 2构建登录原型 我让WebAPI在后台工作,只返回JSON true或false,这取决于传递的凭据是否正确 我得到的身份验证提供程序如下所示: public login(credentials) { if (credentials.email === null || credentials.password === null) { return Observable.throw("P
public login(credentials) {
if (credentials.email === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
this.result = this.http.post(this.CONST.APIUrl, JSON.stringify(credentials), new RequestOptions({headers: this.contentHeader})).map(res => res.json())
if (this.result)
{
this.currentUser = new User('Simon', 'saimon@devdactic.com');
}
return this.result;
}}
public login() {
this.showLoading()
this.auth.login(this.registerCredentials).subscribe(allowed => {
if (allowed) {
setTimeout(() => {
this.loading.dismiss();
this.nav.setRoot(HomePage)
});
} else {
this.showError("Access Denied");
}
},
error => {
this.showError(error);
});
}
登录页面如下所示:
public login(credentials) {
if (credentials.email === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
this.result = this.http.post(this.CONST.APIUrl, JSON.stringify(credentials), new RequestOptions({headers: this.contentHeader})).map(res => res.json())
if (this.result)
{
this.currentUser = new User('Simon', 'saimon@devdactic.com');
}
return this.result;
}}
public login() {
this.showLoading()
this.auth.login(this.registerCredentials).subscribe(allowed => {
if (allowed) {
setTimeout(() => {
this.loading.dismiss();
this.nav.setRoot(HomePage)
});
} else {
this.showError("Access Denied");
}
},
error => {
this.showError(error);
});
}
目前,它总是让人登录。我理解这是因为this.result总是有一个值。但是,在允许用户登录之前,我如何检查API返回的数据呢?您可以使用它为可观察到的应用程序创建副作用
在登录功能中:
public login(credentials) {
if (credentials.email === null || credentials.password === null) {
return Observable.throw("Please insert credentials");
} else {
this.result = this.http.post(this.CONST.APIUrl,
JSON.stringify(credentials), new RequestOptions({headers: this.contentHeader}))
.map(res => res.json())
.do(userData=>{
//check userData and set current user
this.currentUser = new User('Simon', 'saimon@devdactic.com');
return userData;//make sure to return the value to subscribe
});
return result;
}}