我目前正在我的 Angular 6 应用程序中制作树视图,我开始工作(嵌套和所有内容)。我遇到的一个问题是,当我的页面有很多元素(几千个)并且它们都[ngClass]在上面(根据所选节点显示不同的颜色)时,页面往往会挂很多。我创建了一个 StackBlitz 来展示我的问题:https ://stackblitz.com/edit/angular-atveai
要测试它,只需按住右侧输出屏幕上的向上/向下箭头键。它应该很慢。如果您将循环设置为仅显示 100 个元素而不是 10000 个,则它可以完美运行(因为元素较少)。
为了捕捉 keydown 事件(我想要在我的文档中),我这样做:
@Component({
host: {
'(document:keydown)': 'handleKeyboardEvent($event)'
}
})
Run Code Online (Sandbox Code Playgroud)
这handleKeyboardEvent()与$event对象一起调用。
在 HTML 文件中,我有一个非常简单的*ngFor,其中每个元素都有一个[ngClass]="GetClass(item)". 基本上这会返回一个包含所有应该应用的类的对象。在我的情况下,如果所选节点等于元素,则设置obj["selected"] = true,以便一个元素获得selected类。
我可以想象,这要求很高,因为每个元素都会多次调用此方法。这将是我对为什么这很慢的猜测。
我的页面很容易有 5000 到 10000 个节点(如果有办法解决这个问题,我们真的不想改变这一点)。但是,根节点的数量可能在 10 到 30 个之间。许多节点作为子节点嵌套(基本上其中 99% 是嵌套的)。
我的想法是,停止[ngClass]从听的变化,如果父节点不扩大。未展开 = 无论如何都未显示在页面上。
所以真正的问题是:如果满足条件,是否可以阻止元素监听变化?如果是这样,它甚至会有所帮助吗?因为这基本上会引入另一个侦听器,这不会真正解决任何问题。
也许有人知道为什么 Angular 不理解 HttpParamsOptions,或者也许有另一种方法来添加请求头和参数。
`public getJobs(options: {page?: number, size?: number} = {}, database: string): Observable <IJobs[]> {
const headers = new HttpHeaders().set('Dataset-Id', database);
const params: any = {
page: options.page || 0,
size: options.size || 10
};
const httpParams: HttpParamsOptions = { fromObject: params } as HttpParamsOptions;
const headerwithParams = { params: new HttpParams(httpParams), headers: headers };
return this.http.get<IJobs[]>('/api/jobs', headerwithParams);
}`
Run Code Online (Sandbox Code Playgroud) 我正在尝试将以下日期格式化为 Angular 6 中的 MM-DD-YYYY 格式。我也查看了 SO 和其他网站上发布的更简单的问题,但我无法解决它。
我正在使用 Material Angular DatePicker组件。
日期选择器给我
Thu Oct 25 2018 00:00:00 GMT+0400 (Gulf Standard Time)
Run Code Online (Sandbox Code Playgroud)
我想将其格式化为
MM-DD-YYYY
Run Code Online (Sandbox Code Playgroud)
我的代码
<mat-form-field>
<input matInput [min]="minDate" [matDatepicker]="date" [(ngModel)]="request.date | request.date: 'dd/MM/yyyy hh:mm a'" placeholder="Choose a date">
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date></mat-datepicker>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
我也尝试过像下面这样的 DatePipe 模块,但没有任何效果
this.datePipe.transform(this.request.date,"MM-DD-YYYY")
Run Code Online (Sandbox Code Playgroud) 我正在使用:Angular V6.1.0,Angular Material V6.4.1
我正在尝试捕获 HTTP 错误并使用 MatSnackBar 显示它们。我试图在我的应用程序的每个组件中展示这一点(那里有一个 http 请求)。以免做重复的代码
否则,我应该在每个组件中重复相同的代码,以显示插入错误的 MatSnackBar。
这是我的服务:
import { Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
// import { HttpClient, HttpErrorResponse, HttpRequest } from '@angular/common/http';
import { Observable, throwError, of, interval, Subject } from 'rxjs';
import { map, catchError, retryWhen, flatMap } from 'rxjs/operators';
import { url, ErrorNotification } from '../globals';
import { MatSnackBar } from '@angular/material';
import { ErrorNotificationComponent } from '../error-notification/error-notification.component';
@Injectable({
providedIn: 'root'
})
export class XhrErrorHandlerService …Run Code Online (Sandbox Code Playgroud)typescript angular angular-observable angular6 angular-material-6
我想使用这个 Angular 6 代码实现文件下载:
休息API:
private static final Logger LOG = LoggerFactory.getLogger(DownloadsController.class);
@GetMapping(path="export")
public ResponseEntity<byte[]> export() throws IOException {
File pdfFile = Paths.get(EXTERNAL_FILE_PATH).toFile();
byte[] fileContent = Files.readAllBytes(pdfFile.toPath());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
// Here you have to set the actual filename of your pdf
String filename = "output.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
return response;
}
Run Code Online (Sandbox Code Playgroud)
服务:
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs/index";
import {environment} …Run Code Online (Sandbox Code Playgroud) 我有第二种形式。我需要用来自银行的日期填写字段,一个 json 文件。在该populateFields方法中,我获得了一个带有将要填充的数据的参数。但是,它只填充itens数组内的描述值,它不执行。
错误:
错误错误:未捕获(承诺):类型错误:_this.form.controls.itens.value [i] .description.patchValue 不是函数
我尝试使用以下表达式:
this.form.controls.itens.value[i].description.patchValue(item.description);
Run Code Online (Sandbox Code Playgroud)
但是,我收到了上面提到的错误。
generateFormGroup(): void {
this.form = this._formBuilder.group({
id: [null],
alias: ['Relatório de Despesa', [Validators.required, Validators.maxLength(50)]],
job: [null, [Validators.required]],
customer: [{ value: null, disabled: true }, [Validators.required]],
contact: [{ value: null, disabled: true }, [Validators.required]],
currency: [{ value: 1, disabled: true }, [Validators.required]],
exchangeRate: [{ value: null, disabled: true }],
advanceValue: ['0'],
itens: this._formBuilder.array([this.createItem()]),
});
}
createItem(): any {
return this._formBuilder.group({
id: [null],
product: [null, [Validators.required]],
productIcon: …Run Code Online (Sandbox Code Playgroud) 我正在使用具有反应形式的 angular 6。我想动态显示验证消息。我写了一些如下所示的代码。但我接受了这个错误:找不到类型为“对象”的不同支持对象“[对象对象]”。NgFor 仅支持绑定到 Iterables ,例如 Arrays。如何获取输入错误列表?
.html 文件
<form [formGroup]="myForm" (ngSubmit)="save()">
<input type="text" formControlName="studentName">
<div *ngFor="let err of myForm.controls.studentName.errors">
<div>You entered not valid input</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
.ts 文件
this.myForm = this.formBuilder.group({
studentName: ['', Validators.required, Validators.minLength(3)]
});
Run Code Online (Sandbox Code Playgroud) 我有一个组件从资产文件夹加载本地 json 文件(仅包含名称)。由于 HttpClient 负责将数据格式化为 json,所以这里没有使用 map。
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'
export type cont= {name: string};
@Component({
selector: 'my-app',
template: `
1st List
<ul>
<li *ngFor="let contact of contacts | async">{{contact.name}}</li>
</ul>
2st List
<ul>
<li *ngFor="let contact of contacts2 | async">{{contact.name}}</li>
</ul>
`
})
export class AppComponent {;
contacts: Observable<cont[]>;
contacts2: Observable<cont[]>;
constructor (http: HttpClient) {
this.contacts = http.get<cont[]>('http://localhost:4200/assets/contacts.json'); …Run Code Online (Sandbox Code Playgroud) 有没有办法使用 HttpParams 和 fromObject 有条件地添加参数?我尝试在实例化 HttpParams 后添加条件参数,但这不起作用:
const params = new HttpParams({
fromObject : {
requiredParam: 'requiredParam'
}
});
if (addOptionalParam)
params.append('optionalParamKey', 'optionalParamValue');
Run Code Online (Sandbox Code Playgroud)
另外,我可以使用常量变量作为 fromObject 参数的键吗?我试过这个,但它不起作用:
const ConstantVariableForKeyName = 'key';
const params = new HttpParams({
fromObject : {
{{ConstantVariableForKeyName}}: 'paramValue'
}
});
Run Code Online (Sandbox Code Playgroud) 我的应用程序在本地服务时运行良好。我已经编译了没有错误的构建。
网站链接在这里 - https://evilernie44.github.io/users/index.html
当我尝试通过 github 页面加载站点时,我的 get 请求收到 404。
我已经尝试添加一个 404.html,然后按照 angular docs 的建议将 index.html 的内容复制到它,但我似乎仍然无法让它工作。
我不知道接下来要做什么,有没有人有任何建议。