我目前正在开发我的第一个 Ionic 2 应用程序,但我还不是很喜欢打字稿..
我想在我的构造函数中调用 authenticate() 方法,然后调用
打字稿:
export class WelcomePage {
public data: string;
username: string;
password: string;
constructor(public navCtrl: NavController, public http: Http) {
this.authenticate();
}
authenticate() {
var creds = { username: 'user1', password: 'pw1' };
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://www.xyz.api.php', creds, {
headers: headers
})
.map(res => res.json())
.subscribe(
data => this.data,
err => this.logError(err),
() => console.log('Completed')
);
}
}
我已经从 API 得到了响应:
{ "Person":[ {"Username":"user1","Password":"pw1"} ] }
HTML:
<textarea>here: {{data}}</textarea>
--> 文本区域为空
您可能想要提取数组内的对象,该对象位于Person
:
.map(res => res.json().Person[0]) // extract object
此外,您需要this.data
像这样分配数据:
.subscribe(
data => this.data = data // here!
....
然后使用(与此处的安全导航操作员 一起使用?
。)
{{data?.Username}}
和 {{data?.Password}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句