gpt4 book ai didi

javascript - 如何以 Angular 8 显示转换后的值?

转载 作者:行者123 更新时间:2023-11-30 13:44:15 24 4
gpt4 key购买 nike

在我的项目中,我将 .net core 2.2 用于后端,将 angular 8 用于前端。场景是我在后端有一些具有一定范围的整数值。例如:

    [Required]
[Range(1073741824, 1099511627776)] // 1GB -> 1TB
public long DiskSize { get; set; }

为了显示值,我正在使用 ng-slider5我的 typescript 文件如下所示:

diskOptions: Options = {
floor: 1073741824,
ceil: 1099511627776
};

我的 html 文件如下所示:

 <div class="input-group mt-1">
<div class="input-group-prepend">
<div class="input-group-text">
<i class="fas fa-hdd text-primary"></i>&nbsp;Disk Size
</div>
</div>
<ng5-slider [(value)]="model.diskSize" name="diskSize" [(ngModel)]="model.diskSize"
[options]="diskOptions" class="form-control"></ng5-slider>
</div>

我想在 slider 中实现,显示的不是值而是 1gb 递增 1gb 直到 1tb。1024gb 时也显示 gb 并更改 tb。 slider 看起来像:

Disk Size

最佳答案

我从这个 repository 找到了这个管道.尝试在您的 Angular 应用程序中实现它。

bytes.pipe.ts

import { Pipe, PipeTransform, NgModule } from '@angular/core';
import { isNumberFinite, isPositive, isInteger, toDecimal } from '../utils/utils';

export type ByteUnit = 'B' | 'kB' | 'KB' | 'MB' | 'GB' | 'TB';

@Pipe({
name: 'bytes',
})
export class BytesPipe implements PipeTransform {
static formats: { [key: string]: { max: number; prev?: ByteUnit } } = {
B: { max: 1024 },
kB: { max: Math.pow(1024, 2), prev: 'B' },
KB: { max: Math.pow(1024, 2), prev: 'B' }, // Backward compatible
MB: { max: Math.pow(1024, 3), prev: 'kB' },
GB: { max: Math.pow(1024, 4), prev: 'MB' },
TB: { max: Number.MAX_SAFE_INTEGER, prev: 'GB' },
};

transform(input: any, decimal: number = 0, from: ByteUnit = 'B', to?: ByteUnit): any {
if (!(isNumberFinite(input) && isNumberFinite(decimal) && isInteger(decimal) && isPositive(decimal))) {
return input;
}

let bytes = input;
let unit = from;
while (unit !== 'B') {
bytes *= 1024;
unit = BytesPipe.formats[unit].prev!;
}

if (to) {
const format = BytesPipe.formats[to];

const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);

return BytesPipe.formatResult(result, to);
}

for (const key in BytesPipe.formats) {
if (BytesPipe.formats.hasOwnProperty(key)) {
const format = BytesPipe.formats[key];
if (bytes < format.max) {
const result = toDecimal(BytesPipe.calculateResult(format, bytes), decimal);

return BytesPipe.formatResult(result, key);
}
}
}
}

static formatResult(result: number, unit: string): string {
return `${result} ${unit}`;
}

static calculateResult(format: { max: number; prev?: ByteUnit }, bytes: number) {
const prev = format.prev ? BytesPipe.formats[format.prev] : undefined;
return prev ? bytes / prev.max : bytes;
}
}

@NgModule({
declarations: [BytesPipe],
exports: [BytesPipe],
})
export class NgBytesPipeModule {}

工具

export type CollectionPredicate = (item?: any, index?: number, collection?: any[]) => boolean;

export function isUndefined(value: any): value is undefined {
return typeof value === 'undefined';
}

export function isNull(value: any): value is null {
return value === null;
}

export function isNumber(value: any): value is number {
return typeof value === 'number';
}

export function isNumberFinite(value: any): value is number {
return isNumber(value) && isFinite(value);
}

// Not strict positive
export function isPositive(value: number): boolean {
return value >= 0;
}

export function isInteger(value: number): boolean {
// No rest, is an integer
return value % 1 === 0;
}

export function isNil(value: any): value is null | undefined {
return value === null || typeof value === 'undefined';
}

export function isString(value: any): value is string {
return typeof value === 'string';
}

export function isObject(value: any): boolean {
return value !== null && typeof value === 'object';
}

export function isArray(value: any): boolean {
return Array.isArray(value);
}

export function isFunction(value: any): boolean {
return typeof value === 'function';
}

export function toDecimal(value: number, decimal: number): number {
return Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal);
}

export function upperFirst(value: string): string {
return value.slice(0, 1).toUpperCase() + value.slice(1);
}

export function createRound(method: string): Function {
// <any>Math to suppress error
const func: any = (<any>Math)[method];
return function(value: number, precision: number = 0) {
if (typeof value === 'string') {
throw new TypeError('Rounding method needs a number');
}

if (typeof precision !== 'number' || isNaN(precision)) {
precision = 0;
}

if (precision) {
let pair = `${value}e`.split('e');
const val = func(`${pair[0]}e` + (+pair[1] + precision));

pair = `${val}e`.split('e');
return +(pair[0] + 'e' + (+pair[1] - precision));
}

return func(value);
};
}

export function leftPad(str: string, len: number = 0, ch: any = ' ') {
str = String(str);
ch = toString(ch);
let i = -1;
const length = len - str.length;

while (++i < length && str.length + ch.length <= len) {
str = ch + str;
}

return str;
}

export function rightPad(str: string, len: number = 0, ch: any = ' ') {
str = String(str);
ch = toString(ch);

let i = -1;
const length = len - str.length;

while (++i < length && str.length + ch.length <= len) {
str += ch;
}

return str;
}

export function toString(value: number | string) {
return `${value}`;
}

export function pad(str: string, len: number = 0, ch: any = ' '): string {
str = String(str);
ch = toString(ch);
let i = -1;
const length = len - str.length;

let left = true;
while (++i < length) {
const l = str.length + ch.length <= len ? str.length + ch.length : str.length + 1;

if (left) {
str = leftPad(str, l, ch);
} else {
str = rightPad(str, l, ch);
}

left = !left;
}

return str;
}

export function flatten(input: any[], index: number = 0): any[] {
if (index >= input.length) {
return input;
}

if (isArray(input[index])) {
return flatten(input.slice(0, index).concat(input[index], input.slice(index + 1)), index);
}

return flatten(input, index + 1);
}

export function getProperty(value: { [key: string]: any }, key: string): any {
if (isNil(value) || !isObject(value)) {
return undefined;
}

const keys: string[] = key.split('.');
let result: any = value[keys.shift()!];

for (const key of keys) {
if (isNil(result) || !isObject(result)) {
return undefined;
}

result = result[key];
}

return result;
}

export function sum(input: Array<number>, initial = 0): number {
return input.reduce((previous: number, current: number) => previous + current, initial);
}

// http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript
export function shuffle(input: any): any {
if (!isArray(input)) {
return input;
}

const copy = [...input];

for (let i = copy.length; i; --i) {
const j = Math.floor(Math.random() * i);
const x = copy[i - 1];
copy[i - 1] = copy[j];
copy[j] = x;
}

return copy;
}

export function deepIndexOf(collection: any[], value: any) {
let index = -1;
const length = collection.length;

while (++index < length) {
if (deepEqual(value, collection[index])) {
return index;
}
}

return -1;
}

export function deepEqual(a: any, b: any) {
if (a === b) {
return true;
}

if (!(typeof a === 'object' && typeof b === 'object')) {
return a === b;
}

const keysA = Object.keys(a);
const keysB = Object.keys(b);

if (keysA.length !== keysB.length) {
return false;
}

// Test for A's keys different from B.
var hasOwn = Object.prototype.hasOwnProperty;
for (let i = 0; i < keysA.length; i++) {
const key = keysA[i];
if (!hasOwn.call(b, keysA[i]) || !deepEqual(a[key], b[key])) {
return false;
}
}

return true;
}

export function isDeepObject(object: any) {
return object.__isDeepObject__;
}

export function wrapDeep(object: any) {
return new DeepWrapper(object);
}

export function unwrapDeep(object: any) {
if (isDeepObject(object)) {
return object.data;
}

return object;
}

export class DeepWrapper {
public __isDeepObject__: boolean = true;

constructor(public data: any) {}
}

export function count(input: any): any {
if (!isArray(input) && !isObject(input) && !isString(input)) {
return input;
}

if (isObject(input)) {
return Object.keys(input).map(value => input[value]).length;
}

return input.length;
}

export function empty(input: any): any {
if (!isArray(input)) {
return input;
}

return input.length === 0;
}

export function every(input: any, predicate: CollectionPredicate) {
if (!isArray(input) || !predicate) {
return input;
}

let result = true;
let i = -1;

while (++i < input.length && result) {
result = predicate(input[i], i, input);
}

return result;
}

export function takeUntil(input: any[], predicate: CollectionPredicate) {
let i = -1;
const result: any = [];
while (++i < input.length && !predicate(input[i], i, input)) {
result[i] = input[i];
}

return result;
}

export function takeWhile(input: any[], predicate: CollectionPredicate) {
return takeUntil(input, (item: any, index: number, collection: any[]) => !predicate(item, index, collection));
}

关于javascript - 如何以 Angular 8 显示转换后的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com