我试过代码如下:
@Component({
selector: 'test-content',
template: '
<div *ngIf="sv.name.notExist.testUndefined != undefined">
{{sv.name.notExist.testUndefined}}
',
directives: [FORM_DIRECTIVES]
})
Run Code Online (Sandbox Code Playgroud)
变量sv.name.notExist.testUndefined是未定义的,但我用*ngIf检查它,结果是错误消息:"TypeError:无法读取[sv.name.notExist.testUndefined中未定义的属性'testUndefined'!= undefined在......"
请帮我用*ngIf检查变量undefined.
我正在研究的系统由许多分布式微服务组成,每个组件的潜在多个版本可能同时处于活动状态.
我试图构建的Angular2应用程序应该能够通过websockets与这些组件中的每一个进行交互.因为为每个组件的所有未来版本和功能,相应的协议实现甚至新组件准备此应用程序似乎是不可行的,所以我想将此责任推给组件本身.
每个组件都能够通过在同一个websocket连接上发送的包传递其功能(以NG2组件的形式)以及协议实现和必要的GUI元素(HTML/CSS).
是否有一种模式可以在ng2中实现这种"按需加载"组件及其模板?
我要这个:
<div *ngIf="...">div 1...</div>
<div *ngIf="...">div 2...</div>
<div *ngIf="...">div 3...</div>
Run Code Online (Sandbox Code Playgroud)
但我不想重复*ngIf,所以我<my-component>用这个模板创建了我的组件:
<div>div 1...</div>
<div>div 2...</div>
<div>div 3...</div>
Run Code Online (Sandbox Code Playgroud)
我把*ngIf放在我的组件标签中: <my-component *ngIf="...">
问题是Angular 2将<my-component>标记放在DOM中,我不想要它.
对于任何了解ASP.NET WebForms的人,我希望Angular 2中的一个组件像<asp:PlaceHolder>控件一样工作......
我的标记看起来像这样:
<li *ngFor="let user of usersList | orderBy : ['name']; let i = index;">
Run Code Online (Sandbox Code Playgroud)
那么无论如何只有当某个布尔值为真时才应用orderBy管道?
我正在尝试创建CanDeactivate防护,以在用户离开路线时提示“保存更改”。有一些我想排除的路线(可以说,当用户切换选项卡,但不选择新项目时)。我一直在努力了解如何在不使用某种导航服务的情况下获取目标URL,而只保留完整的URL。这是我的签名,可以取消激活
// This is what handles the deactivate
public canDeactivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
Run Code Online (Sandbox Code Playgroud)
所有的“ route”和“ state”对象都包含先前的URL,而没有有关目标URL的信息。
我触发另一个组件中的导航
this._router.navigate(['/clients', this.selectedClient.id, page]);
Run Code Online (Sandbox Code Playgroud) angularjs angularjs-directive angular2-template angular2-routing angular
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { routing } from './app.routing';
import { HttpModule, JsonpModule } from '@angular/http';
import { LoginComponent } from './auth/login.component';
import { SignupComponent } from './register/signup.component';
import { HomeComponent } from './home/home.component';
import { SpinnerComponent} from './uiComponents/page-spinner/Spinner-Component';
import { SpinnerService} from './uiComponents/page-spinner/spinner-service';
import { authProviders } from './auth/auth.providers';
import { LocalStorageService} from './state/local-storage.service';
import {CommonModule} from …Run Code Online (Sandbox Code Playgroud) 有没有办法从模型中自动创建FormGroup?如果我有一个具有多个属性的模型:
型号:人
firstName: string,
lastName: string,
street: string
country: string
....
Run Code Online (Sandbox Code Playgroud)
我想从中创建一个简单的FormGroup:
表格:FormGroup
firstName: FormControl,
lastName: FormControl,
street: FormControl,
country: FormControl
....
Run Code Online (Sandbox Code Playgroud)
对我来说似乎是"脏"的,为模型中的每个属性明确定义FormControl/FormGroup/FormArray:
formBuilder.group({
firstName: person.firstName,
lastName: person.lastName,
street: person.street,
country: person.country,
...
});
Run Code Online (Sandbox Code Playgroud)
每次来自后端的API发生变化时,我都要调整模型和表单映射.是否有某种生成器可以帮助我自动映射/创建FormGroup?
validation typescript angular2-forms angular2-template angular
尝试将数据绑定到下拉列表,但不绑定任何内容,下拉列表显示 NOTHING SELECTED.
<select #classProductTypeCombobox
name="classProductTypeCombobox"
class="form-control col-md-3"
[(ngModel)]="classification.codeType"
[attr.data-live-search]="true"
jq-plugin="selectpicker"
required>
<option *ngFor="let classType of classificationTypes"
[value]="classType">{{classType}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
角度代码:
getClassificationTypes(): void {
//need to remove hard coding
this._commonService.getLookupItems(1, 6).subscribe((result) => {
this.classificationTypes= result.items;
});
}
ngOnInit(): void {
this.getClassificationTypes();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试调试代码时,classificationTypes有适当的数据,我用作硬编码值的相同数据.它工作正常.
方法getClassificationTypes是调用API从数据库中获取数据.
我正在使用ASP.NET Zero框架编写此应用程序.
我尝试了以下解决方案.这是将数据绑定到下拉列表,但是下拉列表的自动搜索功能已经消失,它显示简单的下拉列表.并在控制台中,它提供以下错误消息.
getClassificationTypes(): any {
return this._commonService.getLookupItems(2, 6).subscribe((result) => {
console.log(result.items)
return this.classificationTypes = result.items;
});
}
classificationTypes: TaxonomyItemsLocDto[] = this.getClassificationTypes();
ERROR Error: Cannot find a differ supporting object '[object Object]' …Run Code Online (Sandbox Code Playgroud) 是否有可能写出可重用的ng-template?我的很多组件使用完全相同ng-template.
例如:
<kendo-grid>
<kendo-grid-column field="group">
<ng-template kendoGridEditTemplate let-dataItem="dataItem" let-formGroup="form">
<kendo-dropdownlist #listGroups [data]="groups"
textField="title"
valueField="id"
[valuePrimitive]="true"
[filterable]="true"
[formControl]="form.get('groupId')">
</kendo-dropdownlist>
</ng-template>
</kendo-grid-column>
</kendo-grid>
Run Code Online (Sandbox Code Playgroud)
我不想在我的所有组件中重复这个模板和逻辑.我可以编写自定义组件并将此代码缩小为:
<kendo-grid>
<kendo-grid-column field="group">
<ng-template kendoGridEditTemplate let-dataItem="dataItem" let-formGroup="form">
<my-custom-component [formControl]="form.get('groupId')">
</my-custom-component>
</ng-template>
</kendo-grid-column>
</kendo-grid>
Run Code Online (Sandbox Code Playgroud)
但我想做更多:
<kendo-grid>
<kendo-grid-column field="group">
<ng-template [ngTemplateOutlet]="templateFromAnotherSource">
</ng-template>
</kendo-grid-column>
</kendo-grid>
Run Code Online (Sandbox Code Playgroud)
angular-template angular2-template kendo-ui-angular2 angular
在以下示例中:
https://stackblitz.com/edit/track-by-reuse?file=src/app/app.component.ts
如果将列数从4更改为3,则将td元素更改为1,2由于trackBy函数只有4,7和8而重新渲染,因为它们从一个tr元素移动到下一个元素,因此重复使用,3,5,6和9.
有没有办法跨嵌套的ngFors跟踪元素?这可能是一个功能请求吗?
这里的示例/sf/answers/3171611481/似乎不起作用.