angular - RxJS Map 无法识别更新的数组值
问题描述
编辑:下面的问题是重复的。虽然我尽最大努力阅读其他线程,但我没有考虑范围上下文,因此花了几个小时使用错误的关键字来找到解决方案。一旦错误得到响应,我确保标记有用的响应。无论如何,我的帐户已被阻止。如果管理员可以就我可以做的更多工作提出建议,我将不胜感激。我不知道我还能在这个线程中添加什么。
我正在使用 ng-bootstrap ngbTypeahead:
HTML:
<input id="typeahead-config"
class="form-control"
[ngbTypeahead]="search"
name="muni"
[(ngModel)]="filter.muni"
required/>
TS:
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => term.length < 2 ? []
: this.options.filter(v => v.toLowerCase().startsWith(term.toLocaleLowerCase())).splice(0, 10))
)
我从一个空选项数组开始,然后在 ngOnInit 中调用以下函数:
public getMunicipalities(){
return this.httpClient
.get(
'http://localhost:8080/api/municipalities'
)
.pipe(map((data: any) => data.map(e => e = e.name)))
.subscribe(
function (xhr) {
this.options = xhr;
console.log(this.options);
},
function (err) {
// Log the error
},
);
}
选项数组已更新,但 ngbTypeahead 无法识别新值。在调试期间,我创建了一个硬编码值的替代数组(而不是一个空数组),并发现这些原始值继续显示在 dom 中。
显然我有很多东西要学,但我找不到类似的线程来解释我希望是一个简单的错误......
解决方案
this
vanilla 函数的变化范围。请改用箭头功能
public getMunicipalities(){
return this.httpClient
.get(
'http://localhost:8080/api/municipalities'
)
.pipe(map((data: any) => data.map(e => e = e.name)))
.subscribe(
(xhr) => {
this.options = xhr;
console.log(this.options);
},
(err) => {
// Log the error
},
);
}
推荐阅读
- arrays - 什么算法会给我 O(logd)
- powerbi - 如何删除前导 YYYY-MM-DD 并在 powerbi 中格式化数据类型
- php - 在 Woocommerce 上显示每个产品的发布时间
- android - 如何在 ListView 中显示 JSON 信息(django rest-framework)
- java - 获取 JPanel 的显示区域尺寸,Windows vs Mac
- javascript - javascript函数$.get()被调用时如何加载img和pdf?
- excel - 根据单元格值对动态范围进行排序
- python - 将 NumPy 中定义的函数转换为 SymPy
- windows - ASP.NET Core 忽略 ASPNETCORE_ENVIRONMENT 变量
- sql - 查找两个字符 '(' 和 ')' 之间的值