Angular 如何将数据从角度模型检索到控制器中
饮用水.component.tsAngular 如何将数据从角度模型检索到控制器中,angular,typescript,angular6,angular6-json-schema-form,Angular,Typescript,Angular6,Angular6 Json Schema Form,饮用水.component.ts import { Component } from 'angular/core'; import { userInfoService } from 'some/path'; @Component({ selector: 'drink-water'; templateUrl: './drink-water.component.html' }) export class DrinkWaterComponen
import { Component } from 'angular/core';
import { userInfoService } from 'some/path';
@Component({
selector: 'drink-water';
templateUrl: './drink-water.component.html'
})
export class DrinkWaterComponent implements OnInIt {
...
...
userInfo: userInfoModel;
constructor (public userInfoService : UserInfoService) {}
ngOnInit() {
this.readUsrInfo();
}
readUsrInfo() {
console.log('user info is', this.userInfoService)
//need gender here ..to do some logic
//need gender here..
}
import { Injectable } from '@angular/core';
import { UserInfoModel } from 'some/path';
@Injectable({
providedIn: 'root'
})
export class UserInfoService {
public userInfo: UserInfoModel;
constructor() {}
}
export class UserInfoModel {
gender: string;
userName: string;
hasKids: boolean;
constructor(data: UserInfoModel) {
this.userName = date.userName;
this.gender = data.gender;
}
}
用户信息服务.ts
import { Component } from 'angular/core';
import { userInfoService } from 'some/path';
@Component({
selector: 'drink-water';
templateUrl: './drink-water.component.html'
})
export class DrinkWaterComponent implements OnInIt {
...
...
userInfo: userInfoModel;
constructor (public userInfoService : UserInfoService) {}
ngOnInit() {
this.readUsrInfo();
}
readUsrInfo() {
console.log('user info is', this.userInfoService)
//need gender here ..to do some logic
//need gender here..
}
import { Injectable } from '@angular/core';
import { UserInfoModel } from 'some/path';
@Injectable({
providedIn: 'root'
})
export class UserInfoService {
public userInfo: UserInfoModel;
constructor() {}
}
export class UserInfoModel {
gender: string;
userName: string;
hasKids: boolean;
constructor(data: UserInfoModel) {
this.userName = date.userName;
this.gender = data.gender;
}
}
用户信息.model.ts
import { Component } from 'angular/core';
import { userInfoService } from 'some/path';
@Component({
selector: 'drink-water';
templateUrl: './drink-water.component.html'
})
export class DrinkWaterComponent implements OnInIt {
...
...
userInfo: userInfoModel;
constructor (public userInfoService : UserInfoService) {}
ngOnInit() {
this.readUsrInfo();
}
readUsrInfo() {
console.log('user info is', this.userInfoService)
//need gender here ..to do some logic
//need gender here..
}
import { Injectable } from '@angular/core';
import { UserInfoModel } from 'some/path';
@Injectable({
providedIn: 'root'
})
export class UserInfoService {
public userInfo: UserInfoModel;
constructor() {}
}
export class UserInfoModel {
gender: string;
userName: string;
hasKids: boolean;
constructor(data: UserInfoModel) {
this.userName = date.userName;
this.gender = data.gender;
}
}
饮用水组件的控制台输出->readUsrInfo(Console.log())
请将>替换为向下实心箭头作为json结构。我无法格式化它
问题
如果我试图从控制器中仅控制台this.userInfoService,则会得到上面的控制台输出。如何在readUsrInfo方法中访问名称和性别?this.userInfoService.gender返回未定义
非常感谢您的帮助。您能发布您的用户信息服务的代码吗?@DPro请查看更新的代码。我添加了服务和型号代码。Http请求在其他地方完成,请尝试this.userInfoService.userInfo.gender。如果您收到错误,无法读取未定义的属性'SEXT',则表示您的用户信息尚未初始化。@DPro…感谢您的回复…是的,我无法读取未定义的属性'SEXT'。我如何初始化?你说http请求是在别处完成的,它不应该初始化它吗?
export class UserInfoModel {
userName= 'peggy'
gender = 'female'
...
...
}
userInfo = new UserInfoModel()
console.log('user gender', userInfo.gender)