如何将用户输入存储为打字稿中的对象键?

时间:2018-09-26 23:24:32

标签: javascript angular typescript google-cloud-firestore key-value

对于Firestore来说,我是一个新手,我应该像这样对它进行建模:enter link description here

members {
          id: xyz
                  {
                    name: Jones;
                    hashtag: {
                              global: true,
                              digital: true
                             }
          ...
         }

因此,我想将“ hashtag”表单字段的用户输入存储为hashtag属性的键,并使用Angular 6(Typescript)将值“ true”自动分配给该键。我目前的做法是:

submit(newName: string, newHashtag: string) {
    if ( newName !== '' && newHashtag !== '') {
      this.member.name = newName;
      this.member.hashtag = { newHashtag: true};
      console.log(this.member);
      // this.membersService.addMember(this.member);
    }
  }

结果,程序始终打印出“ newHashtag”,但不打印出所需的输入值。我该如何解决?

export interface Member {
    id?: string;
    name?: string;
    hashtag?: object;
}
<form>
  <input type="text" placeholder="new Name" #memberName name="name">
  <input type="text" placeholder="new hashtag" #hashtagName name="name">
  <button (click)="submit(memberName.value, hashtagName.value);
   memberName.value=''">Submit</button>
</form>>

1 个答案:

答案 0 :(得分:1)

与JavaScript相同:

const property = 'foo'
const object = {}
object[property] = 'desired value'