typescript

Angular - How to use sum and group by

旧街凉风 提交于 2021-02-20 00:20:44
问题 I need to group the records based on ID and display sum of weight. can someone please let me know the sum and group by method in Angular. API Response: data = [ {Id:1, name: 'ABC', weight: 10 }, {Id:1, name: 'ABC', weight: 14 }, {Id:1, name: 'ABC', weight: 16 }, {Id:2, name: 'DEF', weight: 23 }, {Id:2, name: 'DEF', weight: 22 }, {Id:4, name: 'GHI', weight: 44 }, {Id:4, name: 'GHI', weight: 41 } ] Expected output: dataResult = [ {Id:1, name: 'ABC', weight: 40 }, {Id:2, name: 'DEF', weight: 45

TypeScript: Infer type of generic keyof

ぐ巨炮叔叔 提交于 2021-02-20 00:20:25
问题 I have types like this: type GenericType<T, K extends keyof T = keyof T> = { name: K; params: T[K] } type Params = { a: 1; b: 2; } const test: GenericType<Params> = { name: "a", params: 2 } When I create an object like test that has property name: "a" I want the type of params to be inferred so that params must be 1 . In my example params has type 1 | 2 which is Params[keyof Params] . But since name is "a" I think it should be possible to limit the type of params to just 1 without specifying

TypeScript: Infer type of generic keyof

断了今生、忘了曾经 提交于 2021-02-20 00:20:19
问题 I have types like this: type GenericType<T, K extends keyof T = keyof T> = { name: K; params: T[K] } type Params = { a: 1; b: 2; } const test: GenericType<Params> = { name: "a", params: 2 } When I create an object like test that has property name: "a" I want the type of params to be inferred so that params must be 1 . In my example params has type 1 | 2 which is Params[keyof Params] . But since name is "a" I think it should be possible to limit the type of params to just 1 without specifying

Angular - How to use sum and group by

∥☆過路亽.° 提交于 2021-02-20 00:20:13
问题 I need to group the records based on ID and display sum of weight. can someone please let me know the sum and group by method in Angular. API Response: data = [ {Id:1, name: 'ABC', weight: 10 }, {Id:1, name: 'ABC', weight: 14 }, {Id:1, name: 'ABC', weight: 16 }, {Id:2, name: 'DEF', weight: 23 }, {Id:2, name: 'DEF', weight: 22 }, {Id:4, name: 'GHI', weight: 44 }, {Id:4, name: 'GHI', weight: 41 } ] Expected output: dataResult = [ {Id:1, name: 'ABC', weight: 40 }, {Id:2, name: 'DEF', weight: 45

Cannot set initial form values into FormArray

南笙酒味 提交于 2021-02-19 23:55:18
问题 I have a reactive form that on cancel has to set again the initial form values into the formGroup. import { Map } from "immutable"; @Input() data: any; public ngOnInit() { if (this.data && this.data.controls) { this.group = this.fb.group({ isActive: [this.data.isActive], items: this.fb.array(this.buildFormArray(this.data.controlPerformers)), }); // Deep copy of the formGroup with ImmutableJs this.originalFormData = Map(this.group).toJS(); } } public buildFormArray(controllers:

Cannot set initial form values into FormArray

感情迁移 提交于 2021-02-19 23:54:27
问题 I have a reactive form that on cancel has to set again the initial form values into the formGroup. import { Map } from "immutable"; @Input() data: any; public ngOnInit() { if (this.data && this.data.controls) { this.group = this.fb.group({ isActive: [this.data.isActive], items: this.fb.array(this.buildFormArray(this.data.controlPerformers)), }); // Deep copy of the formGroup with ImmutableJs this.originalFormData = Map(this.group).toJS(); } } public buildFormArray(controllers:

Cannot set initial form values into FormArray

半城伤御伤魂 提交于 2021-02-19 23:50:31
问题 I have a reactive form that on cancel has to set again the initial form values into the formGroup. import { Map } from "immutable"; @Input() data: any; public ngOnInit() { if (this.data && this.data.controls) { this.group = this.fb.group({ isActive: [this.data.isActive], items: this.fb.array(this.buildFormArray(this.data.controlPerformers)), }); // Deep copy of the formGroup with ImmutableJs this.originalFormData = Map(this.group).toJS(); } } public buildFormArray(controllers:

document setAttribute ngModel Angular

有些话、适合烂在心里 提交于 2021-02-19 19:44:12
问题 I am trying create an input element from typescript to html in angular 4. But when I try put [(ngModel)] not works. How I do that method ? Someone know how ? createElement() { this.input = document.createElement('input'); this.input.setAttribute('matInput', ''); this.input.setAttribute('placeholder', 'Tema'); this.input.setAttribute('[(ngModel)]', 'module.theme'); this.input.setAttribute('name', 'theme'); return (<HTMLDivElement>(document.getElementById('ejemplo').appendChild(this.input))); }

Typescript overloading functions with union types

情到浓时终转凉″ 提交于 2021-02-19 08:14:06
问题 I want to have a function that takes two union types as parameters but force them to be the same subtype... without having to check the types myself before calling the function. This is what I have tried: The backstory For simplicity I will define some types: type Foo = number; type Bar = [number, number?]; type FooBar = Foo | Bar; I have a function: function fn(x: FooBar, y: FooBar) {} I want to force that x and y be of the same type. IE allow fn(Foo, Foo) and fn(Bar, Bar) but not allow fn

Angular Date type

 ̄綄美尐妖づ 提交于 2021-02-19 08:06:05
问题 From my angular interface date value coming like this. Sat Dec 29 2018 08:42:06 GMT+0400 (Gulf Standard Time) but after receiving data from api JSON result look like this. how to make it same type? 2018-12-27T13:37:00.83 Typescript export interface header{ vr_date : Date } Asp API class public class header { public Nullable<System.DateTime> vr_date { get; set; } } 回答1: You can use Angular's inbuilt DatePipe. So all you need to do is: In Component TS: import { DatePipe } from '@angular/common'