我正在使用一个mat-table来列出用户选择的语言的内容。他们还可以使用对话框面板添加新的语言。在他们加了一种语言回来之后。我希望我的数据源刷新,以显示他们所做的更改。
我通过从服务获取用户数据并将其传递到refresh方法中的数据源来初始化数据存储。
Language.Component.TS
import { Component, OnInit } from '@angular/core';
import { LanguageModel, LANGUAGE_DATA } from '../../../../models/language.model';
import { LanguageAddComponent } from './language-add/language-add.component';
import { AuthService } from '../../../../services/auth.service';
import { LanguageDataSource } from './language-data-source';
import { LevelbarComponent } from '../../../../directives/levelbar/levelbar.component';
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { MatSnackBar, MatDialog } from '@angular/material';
@Component({
selector: 'app-language',
templateUrl: './language.component.html',
styleUrls: ['./language.component.scss']
})
export class LanguageComponent implements OnInit {
displayedColumns = ['name', 'native', 'code', 'level'];
teachDS: any;
user: any;
constructor(private authService: AuthService, private dialog: MatDialog) { }
ngOnInit() {
this.refresh();
}
add() {
this.dialog.open(LanguageAddComponent, {
data: { user: this.user },
}).afterClosed().subscribe(result => {
this.refresh();
});
}
refresh() {
this.authService.getAuthenticatedUser().subscribe((res) => {
this.user = res;
this.teachDS = new LanguageDataSource(this.user.profile.languages.teach);
});
}
}
语言-数据-源.ts
import {MatPaginator, MatSort} from '@angular/material';
import {DataSource} from '@angular/cdk/collections';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/map';
export class LanguageDataSource extends DataSource<any> {
constructor(private languages) {
super();
}
connect(): Observable<any> {
return Observable.of(this.languages);
}
disconnect() {
// No-op
}
}
因此,我尝试调用一个刷新方法,从后端再次获取用户,然后重新初始化数据源。然而,这并不起作用,没有发生任何更改。
通过在refresh()
方法中使用ChangeDetectorRef
触发更改检测刚刚接收到新数据后,在构造函数中注入ChangeDetectorRef并如下所示使用detectChanges:
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { LanguageModel, LANGUAGE_DATA } from '../../../../models/language.model';
import { LanguageAddComponent } from './language-add/language-add.component';
import { AuthService } from '../../../../services/auth.service';
import { LanguageDataSource } from './language-data-source';
import { LevelbarComponent } from '../../../../directives/levelbar/levelbar.component';
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { MatSnackBar, MatDialog } from '@angular/material';
@Component({
selector: 'app-language',
templateUrl: './language.component.html',
styleUrls: ['./language.component.scss']
})
export class LanguageComponent implements OnInit {
displayedColumns = ['name', 'native', 'code', 'level'];
teachDS: any;
user: any;
constructor(private authService: AuthService, private dialog: MatDialog,
private changeDetectorRefs: ChangeDetectorRef) { }
ngOnInit() {
this.refresh();
}
add() {
this.dialog.open(LanguageAddComponent, {
data: { user: this.user },
}).afterClosed().subscribe(result => {
this.refresh();
});
}
refresh() {
this.authService.getAuthenticatedUser().subscribe((res) => {
this.user = res;
this.teachDS = new LanguageDataSource(this.user.profile.languages.teach);
this.changeDetectorRefs.detectChanges();
});
}
}
Material Design for Angular 是 Angular 官方团队开发的基于最新版本 Angular 的 Material Design 风格的框架,可和 Nest.js 搭配使用做全栈开发。 针对 Angular 1 版本的实现 https://www.oschina.net/p/material-design-for-angularjs Angular Material 目
本文向大家介绍Angular6新特性之Angular Material,包括了Angular6新特性之Angular Material的使用技巧和注意事项,需要的朋友参考一下 Angular Material是包含Navigation/Dashboard/Table三种图形类型,这篇文章中将会了解一些其使用的方式。 准备:安装Material 进入到上篇文章创建的demo2,使用ng add进行安
问题内容: 发生更改时,如何检测对数据库的最新更新并以静默方式刷新页面? 假设数据库访问如下所示: 任何想法和样品将不胜感激。谢谢。 问题答案: 这就是我最近使用jQuery实现解决方案的方式。 每当发生 重大 更新时,PHP都会在数据库中增加一个字段。 最初加载页面时,使用数据库中的数字填充JavaScript变量:
IMPORTANT This repository currently contains the code for two versions of the course. The new version of the course is called Angular Material In Depth: You can find the starting point of this new ver
中后台前端应用框架 基于最新Angular 11框架与Material 2技术的企业级中后台前端应用框架。 React => https://github.com/stbui/prophet Plaform Feature 基于 Material Design 精心设计 多种颜色方案,有多个预定义的配色方案 Flex Layout 强大的布局,并支持响应式设计 组件丰富,提炼后台应用的典型页面和场
Material Angular Dashboard Welcome to the first dark dashboard with Google Material Design and Angular! Its much more fun with the demo. Material admin template is absolutely free for commercial usage