我是Angular 5的新手,能够创建使用订阅访问服务方法的视图,但在渲染中遇到延迟问题.用户看到第一个"未定义"字符串,0.5秒后它变为正确的值.
这是组件的.ts文件:
public trips: Trip[];
public numberOfUsers : Map<string, number> = new Map<string, number>();
constructor(private tripService: TripService) { }
ngOnInit() {
this.getTrips();
}
getTrips() {
this.tripService.getTripForMe().subscribe(
data => { this.trips = data;},
err => console.error(err),
() => this.initNumberOfUsers()
);
}
initNumberOfUsers() {
for (let i = 0; i < this.trips.length; i++) {
let count;
this.tripService.getNumberOfUsers().subscribe(
data => { count = data},
err => console.error(err),
() => this.numberOfUsers.set(this.trips[i].id, count)
);
;
}
}
getNumberOfUsers(data) {
return this.numberOfUsers.get(data.id);
}
Run Code Online (Sandbox Code Playgroud)
方法initNumberOfUsers在第一次订阅完成后调用,这是好的,但getNumberOfUsers(数据)可能在之前调用并获得"undefined".
以下是模板的相关部分("data"是ngFor循环的当前值): …
我已经使用 java 16 启动了一个项目,但项目中到处都出现错误:
An error has occurred. See error log for more details.
Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @6dfcffb5
Run Code Online (Sandbox Code Playgroud)
看来我从不同版本的 java 获得的代码在这个特定版本中不起作用。知道我需要在 pojo 类中更改什么吗?我的应用程序中只有 1 个 pojo,因为它是微服务。
谢谢。
我希望在启动时加载数据,例如在使用LocationsService的国家/地区。
我实现了当前服务:
...
@Injectable()
export class LocationsService {
public countries: Country[];
constructor(private http: HttpClient) {
}
public getCountries() {
if (this.countries.length == 0) {
this.http.get<Country[]>(`${this.url}`)
.subscribe(data => { this.countries = data; },
err => console.error(err),
() => console.log(this.countries)
)
};
return this.countries;
}
}
Run Code Online (Sandbox Code Playgroud)
我试图将服务放入引导程序中:
bootstrap: [AppComponent, LocationsService]
Run Code Online (Sandbox Code Playgroud)
但这是行不通的(实际上会引发错误)。我需要这种类型的列表在启动时可用(仅加载1次)。谢谢!
我正在尝试使用文件上传功能将文件保存到文件系统。由于该文件是 Angular 应用程序所必需的,而不是后端(rest api - java)所必需的,因此我决定将其保存在前端应用程序中,这意味着 Angular 应用程序中的 asset 文件夹内的某个位置。
我已经安装了角度文件保护程序。
模板代码:
<input type="file" name="file-7[]" id="file-7" class="inputfile inputfile-6" (change)="handleFileInput($event.target.files)">
Run Code Online (Sandbox Code Playgroud)
组件代码:
import { FileSaver } from 'angular-file-saver';
handleFileInput(files: FileList) {
this.imageUpload = files.item(0);
this.imageFileName = this.logoToUpload.name;
// how to use FileSaver here ?
//this.imageUpload doesn't have data or something like this
}
Run Code Online (Sandbox Code Playgroud)
知道如何将文件实际保存在文件夹中吗?谢谢。
我已成功将材料导入到我的 Angular 8 项目中,并且正在使用 bootstrap 进行样式设置。我不打算使用所有材质库,仅在需要时使用。来自材料的日期选择器是我需要使用的一个示例。
我尝试使用 mat-datepicker 并遇到了切换对齐的问题。
这是模板:
<input type="text" class="form-control" name="my_date" [matDatepicker]="myDatepicker"
style="width: 80px;"
#myDate="ngModel" [(ngModel)]="mydate" id="my_date">
<mat-datepicker-toggle matSuffix [for]="myDatepicker"></mat-datepicker-toggle>
<mat-datepicker #myDatepicker></mat-datepicker>
Run Code Online (Sandbox Code Playgroud)
切换显示并正常工作,但它跳转到新行。我不想使用 mat 形式或 mat 形式字段,因为我正在使用 boostrap。知道如何将切换与文本框的同一行对齐吗?
谢谢。
尝试从 Angular 8 应用程序调用服务时出错。这是服务:
const httpOptionsPlain = {
headers: new HttpHeaders({ 'Accept': 'text/plain',
'Content-Type': 'text/plain'
})
};
@Injectable()
export class TripService {
public getTripNameById(tripId: number): Observable<any> {
return this.http.get<any>(`${this.baseUrl}/Trips/Trip/Name/${tripId}`, httpOptionsPlain);
}
Run Code Online (Sandbox Code Playgroud)
这是 Java rest api(从浏览器调用时工作正常):
@GET
@Path("Trip/Name/{fundId}")
@Produces("text/plain")
public String getTripNameById(@PathParam("tripId") Integer tripId) {
return myDao.getNameById(tripId);
}
Run Code Online (Sandbox Code Playgroud)
我在 chrome 控制台中收到错误消息:
错误:{error: SyntaxError: Unexpected token A in JSON at position 0 at JSON.parse () at XMLHtt ..., text: "AAA BBB CCC"} headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ} 消息:“解析http://localhost:8080/ …
我想创建一个适用于溢出时的指令。我过去曾经使用过dotdotdot jQuery插件,但是在angular 5中它并没有真正起作用。
到目前为止,我正在使用选择器[appDotdotdot]创建一个名为DotdotdotDirective的指令,如下所示:
import { Directive, ElementRef } from '@angular/core';
declare var $: any;
@Directive({
selector: '[appDotdotdot]'
})
export class DotdotdotDirective {
constructor(private el: ElementRef) { }
}
Run Code Online (Sandbox Code Playgroud)
用法很简单:
<h3><a href="#" appDotdotdot>{{data.trip.name}}</a></h3>
Run Code Online (Sandbox Code Playgroud)
我还导入了jQuery插件,以防可以在指令中使用。index.html:
<script src="assets/js/jquery.dotdotdot.js"></script>
Run Code Online (Sandbox Code Playgroud)
我期望代码应该在构造函数中实现,但是我不知道如何在angular 5中使用它?我应该将指令用作jQuery插件的包装器,还是angular对此要求有不同的解决方案?提前致谢!
我有一个带有“ dotdotdot” css类的以下模板,该模板在溢出时正确添加了省略号。
<div class="dotdotdot">{{data.trip.name}}</div>
Run Code Online (Sandbox Code Playgroud)
我在这里要做的是实现一个指令,该指令仅在省略号被激活时才添加工具提示。
这是指令中的当前代码:
import { Directive, OnInit, ElementRef } from '@angular/core';
declare var $: any;
@Directive({
selector: '.dotdotdot'
})
export class DotdotdotDirective implements OnInit {
private el: HTMLElement;
constructor(elRef: ElementRef) {
this.el = elRef.nativeElement;
}
ngOnInit() {
if (this.isEllipsisActive(this.el)) {
// TODO add title attribute to the div with value from text
$(this.el).tooltip();
}
}
isEllipsisActive(e) {
return (e.offsetWidth < e.scrollWidth);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码中有两个问题:
谢谢!
我有以下上传控制器,它有两个不同类型的参数:1 用于保存文件的路径,2 用于文件本身。我正在寻找正确的方法定义,而不是 2 个在 STS 中给出错误的 @Requestparam。
@PostMapping("/{path}/")
public String handleFileUpload(@RequestParam("path"), @RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
filesStorageService.store(file);
redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 get 请求中将 id 列表发送到服务器,我正在执行以下操作:
public loadTripsByIds(tripsIds:number[]): Observable<any> {
let params = new HttpParams();
params = params.append('tripsIds', tripsIds.join(', '));
return this.http.get<TripObj[]>(`${this.baseUrl}/Trips/ByIds/Get`, {params: params});
}
Run Code Online (Sandbox Code Playgroud)
在服务器 api 代码(其余)中,我定义了列表:
@GET
@Path("Trips/ById/Get")
@Produces("application/json")
public List<Trips> loadTripsById(@QueryParam("tripsIds") final List<String> tripsIds) {
Run Code Online (Sandbox Code Playgroud)
我在服务器中实际得到的是一个包含 1 个项目(字符串类型)并以逗号分隔的列表。例如“10001、10002”。我可以轻松地解析服务器端的字符串,但寻找正确的方法将列表发送到服务器,其中每个元素将是 id。
谢谢。
我是 springboot 的新手,并使用 spring.io 创建项目以创建微服务。
使用 spring.io 网站创建项目时,会创建一个包含所有相关依赖项的 pom,但不会添加版本。我是否应该自己添加版本以查看 Maven 存储库 jar(所有 jar 都包含版本)?
angular ×8
spring-boot ×2
css ×1
file ×1
java ×1
jquery ×1
maven ×1
rest ×1
rxjs ×1
typescript ×1