使用Typescript获取Angular 2中的属性
发布时间:2020-12-17 17:01:36 所属栏目:安全 来源:网络整理
导读:我正在尝试使属性fullName显示第一个和最后一个名称.如何让get属性工作? 见这Plunk. import { Component } from '@angular/core';export class Person { id: number; firstName: string; lastName: string; get fullName(): string { return this.firstName
|
我正在尝试使属性fullName显示第一个和最后一个名称.如何让get属性工作?
见这Plunk. import { Component } from '@angular/core';
export class Person {
id: number;
firstName: string;
lastName: string;
get fullName(): string {
return this.firstName + ' ' + this.lastName;
}
}
@Component({
selector: 'my-app',template:`
<h1>{{title}}</h1>
<p>My first name is {{person.firstName}}</p>
<p>My last name is {{person.lastName}}</p>
<h2>My full name is {{person.fullName}}!</h2>`
})
export class AppComponent {
title = 'Get property issue';
person: Person = {
id: 1,firstName: 'This',lastName: 'That'
};
}
编辑 请参阅我更新的plunk 解决方法Working PLUNKER
试试这个 import { Component } from '@angular/core';
export class Person {
constructor(public id: number,public firstName: string,public lastName: string){}
get fullName(): string {
return this.firstName + ' ' + this.lastName;
}
}
@Component({
selector: 'my-app',template:`
<h1>{{title}}</h1>
<p>My first name is {{person.firstName}}</p>
<p>My last name is {{person.lastName}}</p>
<h2>My full name is {{person.fullName}}!</h2>
`
})
export class AppComponent {
title = 'Get property issue';
person: Person = new Person( 1,'This','That');
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
