如何从角度模型检索数据到控制器中

问题描述 投票:0回答:1

drink-water.component.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..

        }

user-info.service.ts

import { Injectable } from '@angular/core';
import { UserInfoModel } from 'some/path';

@Injectable({
providedIn: 'root'
})

export class UserInfoService {
public userInfo: UserInfoModel;

constructor() {}
}

user-info.model.ts

export class UserInfoModel {
gender: string;
userName: string;
hasKids: boolean;
constructor(data: UserInfoModel) {
this.userName = date.userName;
this.gender = data.gender;
}
}

饮用水组件的控制台输出-> readUsrInfo(console.log())请用向下的实心箭头替换>作为json结构。我无法格式化

   > userInfoService {}
    > userInfo : UserInfoModel
      userName: peggy
      gender: female
      hasKids: true
      student: false
      >foods: {vegetables: Array(3), Fruits: Array(5)}
    >_proto_: Object

问题

如果我尝试从控制器仅控制台this.userInfoService,则会获得上述控制台输出。如何在readUsrInfo方法中访问名称和性别? this.userInfoService.gender返回未定义。

感谢您的任何帮助。

angular typescript angular6 angular6-json-schema-form
1个回答
0
投票
export class UserInfoModel {
      userName= 'peggy'
      gender = 'female'
      ...
      ...
}

userInfo = new UserInfoModel()

console.log('user gender', userInfo.gender)
热门问题
推荐问题
最新问题