一段时间以来,我一直在努力解决这个问题。
当我仅在条件为真时尝试在表单中输入时,我收到一个错误,即无法从 undefined 中读取无效。
使用 elvis 运算符时,我不再收到错误消息,但即使显示输入无效和脏,我仍然看不到错误消息。
<form #myForm="ngForm">
... other inputs ...
<input *ngIf="model.type === 'V'"
name="price"
type="number"
required
[(ngModel)]="model.price"
#price="ngModel">
<div class="errors" *ngIf="price?.invalid && price?.dirty">
Problem detected
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
有人暗示我缺少什么吗?
我的要求是将代码中的事件触发到父托管组件.
我在这里使用第一个答案作为参考:angular2手动触发特定元素上的click事件
如果我尝试这个,它运作良好:
this.itemHost.viewContainerRef.element.nativeElement.dispatchEvent(new Event('click'));
Run Code Online (Sandbox Code Playgroud)
在父组件中我写了这个:
(click)="go()"
Run Code Online (Sandbox Code Playgroud)
当上述代码出现时,它到达go方法.
但是,如果我使用一些自定义事件名称,这不起作用,例如:
this.itemHost.viewContainerRef.element.nativeElement.dispatchEvent(new Event('customEvent'));
Run Code Online (Sandbox Code Playgroud)
并在父组件中:
(customEvent)="go()"
Run Code Online (Sandbox Code Playgroud)
如何使用自定义事件进行操作?
我正在尝试以 angular 5 反应形式捕获 FormArray 控件的 valueChanges 事件。我有正常的表单组 valueChanges 事件和相同的工作正常但不适用于 formArray 控件。
我已将我的表单组定义为 -
this.ApplicationForm = this.fb.group({
ApplicationPenalties: this.fb.array([this.preparePenaltyDetails()])
});
Run Code Online (Sandbox Code Playgroud)
并初始化表单数组如下 -
preparePenaltyDetails(): FormGroup {
return this.fb.group({
Id: '',
ApplicationId: '',
RevisionNo: '',
PenaltyMwh: ''
});
}
Run Code Online (Sandbox Code Playgroud)
当我从我的 api 获取数据时,我将控件设置为 -
this.ApplicationForm.setControl('ApplicationPenalties', this.fb.array(this.application.ApplicationPenalties
.map(x => this.fb.group(x)) || []));
Run Code Online (Sandbox Code Playgroud)
我的 html 模板如下 -
<div formArrayName="ApplicationPenalties" *ngFor="let penalty of ApplicationPenalties.controls; let i=index">
<div [formGroupName]="i">
<div class="row pocCharges">
<div class="col-md-6">
<div class="form-group">
<h4 class="d-sm-block">Revision Number</h4>
<div class="input-group">
<input class="form-control form-control-sm" formControlName="RevisionNo" type="text">
<span …Run Code Online (Sandbox Code Playgroud) angular angular-reactive-forms angular-forms formarray angular5
当您输入公式时,我试图在我的应用程序中重现类似于 Microsoft Excel / Google Sheets 的用户体验,并且您可以使用不同的公式和变量来自动完成下拉菜单。
为此,在验证自动完成功能后,我希望能够控制光标的位置。
例如,如果我输入=sum(变量1,变量2),则在变量2自动完成时,光标应该位于最后一个括号之前,而不是在最后。
我了解如何使用javascript设置光标的位置,问题是因为我同时修改输入的值并设置光标位置,后者不起作用。
我用更简单的上下文重现了问题: https ://jsfiddle.net/joparisot/j8ourfa1/31/
我的HTML:
<div id="app">
<autocomplete v-model="selection"></autocomplete>
</div>
<template id="autocomplete">
<div>
<h2>gernerogrnio</h2>
<input id="my-input"
class="form-control"
type="text"
:value="value"
@keydown.enter="updateValue($event.target.value)">
<p>{{ value }}</p>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我的脚本:
Vue.component('autocomplete', {
template: '#autocomplete',
props: {
value: {
type: String,
required: true
}
},
methods: {
updateValue (value) {
var new_value = ''
if (value.length < 4) {
new_value = 'Inferior'
} else {
new_value = 'Superior'
}
this.$emit('input', new_value)
var myInput = document.getElementById('my-input');
this.setCaretPosition(myInput, …Run Code Online (Sandbox Code Playgroud) 我尝试了很多次搜索次数,但我没有得到任何禁用 div 元素点击的解决方案。已经提出问题,但他们说在 div 上不可能,我只想知道,有没有办法在angular2 中禁用 div
<div>(click)="isDisabled ? $event.stopPropagation() : myClickHandler($event); isDisabled ? false : null"
[class.isDisabled]="isDisabled"></div>
Run Code Online (Sandbox Code Playgroud)
关于禁用 div 和指针事件的旧答案并不清楚:旧版本浏览器不支持无,它也可以从网络选项卡进行编辑
我正在构建一个角度应用程序,一切正常,直到导入并实现了“路由模块”
错误信息
我已经经历了,不认为以下是可能的问题:
路由器模块代码:
import { NgModule } from '@angular/core';
import { Routes , RouterModule } from '@angular/router';
import { RecipesComponent } from './recipes/recipes.component';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
const appRoutes: Routes = [
{path: 'recipes', component: RecipesComponent},
{path: 'shopping-list', component: ShoppingListComponent},
{ path: '', redirectTo: 'recipes', pathMatch: 'full'},
{ path: '**', redirectTo: '/not-found'},
]
@NgModule({
imports: [
RouterModule.forRoot(appRoutes),
],
exports: [AppRoutingModule]
})
export class AppRoutingModule{
}Run Code Online (Sandbox Code Playgroud)
根组件代码:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule …Run Code Online (Sandbox Code Playgroud)是的,所以我正在迭代一系列信息,并且这些信息按照我想要的方式显示,但是,我在控制台中收到了一些令人惊讶的错误:ERROR TypeError: "_v.context.$implicit is undefined"
API服务:
private extractData(res: Response) {
let body = res;
return body || {};
}
getWeather(city: string, isoCode: string): Observable<any> {
return this.http.get(`${this.endPoint}${city},${isoCode}${this.constants.apiKey}`)
.pipe(map(this.extractData));
}
Run Code Online (Sandbox Code Playgroud)
使用api服务的组件:
theWeather:any = [];
countryList = COUNTRIES;
isLoading: boolean = true;
showWeather: boolean = false;
constructor(private apiCall:ApiService) { }
ngOnInit() {
this.retrieveWeather()
};
retrieveWeather() {
console.log('COUNTRY LIST', this.countryList);
this.theWeather = [];
this.countryList.map((element, i) => {
this.apiCall.getWeather(element.city, element.countryIso)
.subscribe((data: {}) => {
element.weatherInfo = data;
});
this.isLoading = false;
});
this.showWeather …Run Code Online (Sandbox Code Playgroud) 我有一项服务从服务器获取一些数据并更新一些主题,在我的组件的 ngOnInit 函数中我正在订阅该主题。我已确认主题已正确更新。
以下是我的服务功能,它从服务器获取数据并更新主题。
getInboxData(id?) {
this.inboxID = id;
this.loadingData.next( // tells to start showing loading animation
{
showloading: true,
clearDocuments: this.shouldClearDocuments()
});
this.getInboxCount();
this.dashboardService.getInbox(id, this.page)
.subscribe((response) => {
for (const inbox of response['results']) {
this.documents.push(inbox.document);
// tells to stop showing loading animation
this.loadingData.next({showloading: false, clearDocuments: this.shouldClearDocuments()});
}
if (response.next != null) {
this.page++;
// this.getInboxData(id);
}else {
this.page = 1; // reset
}
this.documentsData.next(response);
});
}
Run Code Online (Sandbox Code Playgroud)
我正在我的组件中订阅documentsData。以下是组件的代码。
ngOnInit() {
this.dashboardDataService.getInboxData();
this.dashboardDataService.documentsData
.subscribe((response) => {
this.isLoadingMoreResult = false; …Run Code Online (Sandbox Code Playgroud) Angulars文档解释说,结构指令,例如<p *ngIf="a as b"></p>“脱糖”到<p [ngIf]="a" [ngIfAs]="b">.
脱糖使用microsyntax,允许表达式如下
let node; when: hasChild
a as b
let x of y; index = i; trackBy: f
Run Code Online (Sandbox Code Playgroud)
该文档提供了一些微语法示例,并建议研究 的来源ngIf,但没有提供正式定义。
angular 结构指令的微语法的语法是什么?
angular ×8
javascript ×2
angular5 ×1
caret ×1
events ×1
formarray ×1
forms ×1
html ×1
input ×1
java ×1
observable ×1
rxjs ×1
spring-boot ×1
spring-mvc ×1
typescript ×1
vue.js ×1