简体   繁体   中英

Angular Material Example in TypeScript: Chips

I'd like to recreate the chips example seen here ( https://material.angularjs.org/latest/#/demo/material.components.chips ) in TypeScript, and as I'm fairly new (started learning it this week!), I'm a bit stuck with re-writing this from Javascript into TypeScript:

JS:

function mockLabels() {
      var labels = [
        {
          'name': 'hotel',
          'id': '1'
        },
        {
          'name': 'sport',
          'id': '2'
        },
        {
          'name': 'gaming',
          'id': '3'
        }           
      ];
      return labels.map(function (lab) {
        lab._lowername = lab.name.toLowerCase();
        return lab;
      });
    }

TypeScript:

private mockLabels: Array<Label> = [
            new Label(1, 'hotel'),
            new Label(2, 'sport'),
            new Label(3, 'gaming')
        ];

public getLabels() {
            return this.mockLabels;         
        }

As you can see, I'm not sure how to include .map in my TypeScript code when it's been assigned to an array in TypeScript.

How would I rewrite mockLabels to do so?

Note that JavaScript is TypeScript . So I would do the following (basically just add type annotations):

function mockLabels() {
  var labels:{
    name: string;
    id: string;
    _lowername?: string;
  }[] = [
    {
      'name': 'hotel',
      'id': '1'
    },
    {
      'name': 'sport',
      'id': '2'
    },
    {
      'name': 'gaming',
      'id': '3'
    }
  ];

  return labels.map(function (lab) {
    lab._lowername = lab.name.toLowerCase();
    return lab;
  });
}

Note : you had veg but the variable wasn't defined, I changed it to lab assuming that is what you meant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM