加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用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'
  };
}

编辑
我实际想要实现的是如何在调用服务和订阅结果时使用get属性.但我设法根据以下答案搞清楚.谢谢!

请参阅我更新的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');
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读