沈斌
2018-07-12 1dfc8bc300a6ae0822514ddf2e1399d4762fa1b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { FormGroup, FormArray, AbstractControl } from '@angular/forms';
import { Injectable } from '@angular/core';
 
@Injectable()
export class ToolsService {
      public static markAsDirty(controlSet: FormGroup|FormArray) {
         const controls = controlSet.controls;
         Object.values(controls).forEach(
             (control: AbstractControl) => {
                    if (control instanceof FormGroup || control instanceof FormArray) {
                        ToolsService.markAsDirty(control);
                    } else {
                          control.markAsDirty();
                    }
             }
         );
      }
      public static getValueFormControl(controlSet: FormGroup, name: string) {
        return controlSet.controls[name].value;
      }
      public static setValueToControl(controlSet: FormGroup, name: string, value: any) {
         controlSet.controls[name].setValue(value);
         controlSet.controls[name].updateValueAndValidity();
      }
      public static removePrivate(obj: object) {
            Object.keys(obj).forEach(
                 (key: string) => {
                        if (key.startsWith('_')) {
                            delete obj[key];
                        } else {
                            if (obj[key] instanceof Object) {
                                ToolsService.removePrivate(obj[key]);
                            }
                        }
                 }
                );
      }
      public static toThousands(number: string): string {
        let num = (number || 0).toString(), result = '';
        while (num.length > 3) {
            result = ',' + num.slice(-3) + result;
            num = num.slice(0, num.length - 3);
        }
        if (num) { result = num + result; }
        return result;
    }
    public static getObjById<T>(id: number, list: T[]): T {
        return list.find(item => item['id'] === id);
    }
}