我在我的材质表中加载数据:
ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}
Run Code Online (Sandbox Code Playgroud)
我想在加载时显示微调器: <mat-spinner ></mat-spinner>
我试试:showSpinner:boolean = true;
ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mat-autocomplete类似于以下示例的过滤器;
所以我试图实现这个功能,这样当用户开始输入交易时,他们正在寻找基于字符串中任何地方的部分字符串匹配的过滤器,并在选项中突出显示.
我目前在我的.html中
<mat-form-field class="form-group special-input">
<input type="text" placeholder="Select a trade" aria-label="Select a trade" matInput [formControl]="categoriesCtrl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete" md-menu-class="autocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.name">
{{ option.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
我的.ts在哪里
categoriesCtrl: FormControl;
filteredOptions: Observable<ICategory[]>;
options: ICategory[];
categorySubscription: Subscription;
constructor(fb: FormBuilder, private router: Router, private service: SearchService, private http: Http) {
this.categoriesCtrl = new FormControl();
}
ngOnInit() {
this.categorySubscription = this.service.getCategories().subscribe((categories: ICategory[]) => {
this.options = categories;
this.filteredOptions = this.categoriesCtrl.valueChanges
.pipe(
startWith(''),
map(options …Run Code Online (Sandbox Code Playgroud) 我正在构建一个必须使用 http 的 Angular5 项目。我在这里使用 observable,但是当我使用 'of' 时
scope.fetchApi = Observable.of(data);
Run Code Online (Sandbox Code Playgroud)
然后它给了我一个错误:
Property 'of' does not exist on type 'typeof Observable'.
Run Code Online (Sandbox Code Playgroud)
我也导入了'rxjs/add/observable/of';,但也有同样的错误。我也试过,import { Observable } from 'rxjs/Observable';但它抛出了一个错误:
Module '"eclipse:angular5-example/node_modules/rxjs/Observable"' has no exported member 'Observable'.
Run Code Online (Sandbox Code Playgroud)
你可以看到我的代码如下:
import { Component, OnInit } from '@angular/core';
import { UserService } from './app.service';
import { FetchApi } from '../models/fetch-api.model';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import 'rxjs/add/observer/of';
@Component({
selector: 'app-fetch-api',
templateUrl: './fetch-api.component.html',
styleUrls: ['./fetch-api.component.css']
}) …Run Code Online (Sandbox Code Playgroud) 我最近遇到了一个看起来像这样的东西:
interface Test<T extends Test<T>> {
a: number;
b: T;
}
function foo <T extends Test<T>>(el: T): T {
...
}
Run Code Online (Sandbox Code Playgroud)
我必须说这到底是什么,以及为什么需要这样的东西有些困惑。我浏览过Typescript手册的“ 泛型”部分,但找不到类似的内容。
该接口实现了什么,而用以下类似的东西无法完成?
interface Test<T>
Run Code Online (Sandbox Code Playgroud)
谁能对此有所启发?
最近,我不得不优化与创建大型数组(〜10?元素)有关的任务。
我测试了几种不同的方法,并且据jsperf所述,以下选项似乎是最快的。
var max = 10000000;
var arr = new Array(max);
for (let i = 0; i < max; i++) {
arr[i] = true;
}
Run Code Online (Sandbox Code Playgroud)
比〜快85%
var max = 10000000;
var arr = [];
for (let i = 0; i < max; i++) {
arr.push(true);
}
Run Code Online (Sandbox Code Playgroud)
实际上,在我的实际应用中,第一个代码片段的运行速度也要快得多。
然而,我的理解是,V8引擎能够进行优化的操作对数组PACKED_SMI_ELEMENTS的元素种类,而不是数组HOLEY_ELEMENTS。
所以我的问题是:
new Array(n)创建一个内部标记为的数组HOLEY_ELEMENTS(我相信是真的),并且[]创建一个内部标记有数组的数组PACKED_SMI_ELEMENTS(我不太确定是真的)为什么第一个摘要比第二个要快?
我经历过的相关问题:
假设我有以下对象:
const original = {
first: 1,
second: 2,
third: 3
}
Run Code Online (Sandbox Code Playgroud)
我想用以下结构创建一个新的,不同的对象:
const modified = {
first: 100,
third: 3
}
Run Code Online (Sandbox Code Playgroud)
ES6语法允许我做一些非常强大的操作,如:
const {second, ...newElement} = original
这导致:
const newElement = {
first: 1,
third: 3
}
Run Code Online (Sandbox Code Playgroud)
但后来我还是要做newElement.first = 100.
或者,我可以这样做:
const newElement2 = Object.assign({}, original , {second: undefined, first: 100})
但这并没有真正删除second,只是将其设置为undefined.
是否有一个更优雅的替代,从去original到modified?
angular ×3
typescript ×3
javascript ×2
angular5 ×1
arrays ×1
datasource ×1
ecmascript-6 ×1
generics ×1
performance ×1
rxjs ×1
spinner ×1
v8 ×1