如何将数据导入Angular中的另一个模型?

时间:2019-06-22 04:42:27

标签: angular

我是Angular的新手,并有一些急切的问题。由于我没有这种语言的经验,希望大家都能理解我的要求。

我浏览了书籍和其他资料,但似乎找不到正确的答案

在我的user.ts文件中,我声明了User类

export class User {
  constructor(public id:number, public name: string, public image: string, public hobbies: any ) {}
}

在我的hobbies.ts中,我声明了Hobbies类

export class Hobby {
  constructor(public id:number, public hobbyType:string, public hobbyDesc:string) {}
}

在hobby-list.ts中有一个兴趣爱好列表

export const HOBBIES: Hobby[] = [{id:1, hobbyType:"Snorkeling", hobbyDesc:"Snorkeling is the practice of swimming on or through a body of water while equipped with a diving mask, a shaped breathing tube called a snorkel, and usually swimfins. In cooler waters, a wetsuit may also be worn."},
{id:2, hobbyType:"Diving", hobbyDesc:"Scuba diving is a mode of underwater diving where the diver uses a self-contained underwater breathing apparatus, which is completely independent of surface supply, to breathe underwater."}
]

我在另一个ts中列出了User数组

import {Hobby} from './hobby.ts';
import {HOBBIES} from './hobby-list.ts';
export const USERS: User[] = [{id:1, name:"Mandy",image:"mandy.jpg",hobbies: <not sure how to get the hobby type listing from Hobbies-list.ts>}
]

因此,预期的结果将是在User.componment.html中显示“用户”列表,并显示用户的兴趣爱好 例如。 姓名:曼迪<br> 图片:mandy.jpg <br> 兴趣爱好:水肺潜水

我认为html代码会是这样吗?

<h2>All Users</h2>
<ul *ngFor="let user of users">
<li>{{user.name}}</li>
<ul *ngFor="let hob of users.hobby">
<li>{{hob.hobbyType}}</li>
</ul>
</ul>

感谢所有帮助!

3 个答案:

答案 0 :(得分:1)

我认为您在问如何在const USERS中设置兴趣爱好属性。然后首先将“用户类别”爱好属性类型更改为“爱好”数组,如下所示:

export class User {
  constructor(public id:number, public name: string, public image: string, public hobbies: Hobby[] ) {}
}

然后您可以按如下所示在const USERS中分配兴趣爱好属性

import {Hobby} from './hobby.ts';
import {HOBBIES} from './hobby-list.ts';
export const USERS: User[] = [{id:1, name:"Mandy",image:"mandy.jpg",hobbies:HOBBIES }
]

在组件类文件中分配

this.users =USERS // assign the USER constant

在HTML

<div *ngFor="let user of users">
  {{user.name}} <br/>
   <img [src]="user.image"/> <br/>
   Hobbies 
   <ul *ngFor="let hob of users.hobby">
     <li>{{hob.hobbyType}}</li>
   </ul>
</div>

为用户分配一些特定的兴趣爱好。创建类变量。

hobbies :  Hobby[] =HOBBIES ;

现在分配用户

export const USERS: User[] = [
{id:1, name:"Mandy",image:"mandy.jpg",hobbies:[this.hobbies[0] ] },
{id:1, name:"Mandy",image:"mandy.jpg",hobbies:[this.hobbies[0],this.hobbies[1] ] }
]

或者您可以按以下方式分配兴趣爱好

    import {Hobby} from './hobby.ts';
    import {HOBBIES} from './hobby-list.ts';
    export const USERS: User[] = [{id:1, name:"Mandy",image:"mandy.jpg",hobbies:[HOBBIES[0]] },
{id:2, name:"Mandy1",image:"mandy1.jpg",hobbies:[HOBBIES[0],HOBBIES[0]] }

    ]

答案 1 :(得分:0)

问题是您的导入,您进行了结构分解或在模型中使用export default,例如:export default class User {}, 但是只有导出,您需要像下面这样应用解构:

import {Hobby} from './hobby.ts';
import {HOBBIES} from './hobby-list.ts';
export const USERS: User[] = [{id:1, name:"Mandy",image:"mandy.jpg",hobbies: <not sure how to get the hobby type listing from Hobbies-list.ts>}
]

答案 2 :(得分:0)

from bs4 import BeautifulSoup
import requests

url = 'https://patents.google.com/patent/US7054130?oq=US7654309'
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
data = [claim.text for claim in soup.select('.claim .claim')]
print(data)
  

在模板中

 export class Hobby {

     id:number;
     hobbyType:string;
     hobbyDesc:string

    }



export class User {

id:number;
name: string; 
image: string;

hobby: Hobby

}


export class Users {

 user: User [];


}