使用setDate方法修改的日期对象未在模板中更新.
在模板中:
<p>{{date | date:'mediumDate'}}</p>
Run Code Online (Sandbox Code Playgroud)
在组件中:
nextDay(){
this.date.setDate(this.date.getDate()+1);
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用nextDay函数时,模板不会使用新值更新.
我可以让变更检测工作的唯一方法是这样做:
nextDay(){
var tomorrow = new Date();
tomorrow.setDate(this.date.getDate()+1);
this.date = tomorrow;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来完成同样的任务?
我正在编写一些Angular2模板,这些模板具有不同容器的重复部分.在这种情况下,如果事物已分组并且启用了多节模式,则视图可能会更改.请原谅长长的例子,但是这样的话:
<template [ngIf]="isCategoryGrouped">
<div *ngFor="#categories of categories">
<div>{{ categories.category.name }}</div>
<div *ngFor="#thing of categories.things">
<label *ngIf="isMultiSelectMode">
<input type="checkbox" (change)="updateThingSelection(thing, $event)" />
<img [src]="thing.image" /> {{ thing.name }}
</label>
<a href="javascript: void(0)" (click)="selectThing(thing)" *ngIf="! isMultiSelectMode">
<img [src]="thing.image" /> {{ thing.name }}
</a>
</div>
</div>
</template>
<template [ngIf]="! isCategoryGrouped">
<div *ngFor="#thing of things">
<label *ngIf="isMultiSelectMode">
<input type="checkbox" (change)="updateThingSelection(thing, $event)" />
<img [src]="thing.image" /> {{ thing.name }}
</label>
<a href="javascript: void(0)" (click)="selectThing(thing)" *ngIf="! isMultiSelectMode">
<img [src]="thing.image" /> {{ thing.name }}
</a>
</div>
</template> …Run Code Online (Sandbox Code Playgroud) 我需要将现有的Angular 2 RC 1应用程序迁移到Angular 2 RC 4.作为其中的一部分,我还需要将现有的表单移动到Angular 2 RC 4 New Forms.
任何人都可以指导,如何将现有表格更新为新表格.
angular2-forms angular2-directives angular2-template angular
我想知道Angular 2提供了一种确定客户端设备(大屏幕,中等或小屏幕)的方法,或者可能是屏幕宽度,基本上可以显示或隐藏某些内容(从dom中删除).
我在我的应用程序中使用角度2表单,我已根据给定的链接创建表单.
https://angular.io/docs/ts/latest/guide/forms.html
在这个用于验证和使用表单API,我已设置ngModel值#name="id" #id="ngModel"和哪些抛出脚本错误.但如果我设定#id="ngModel"为,它就解决了 #id="ngForm".但对于我的情况,我必须将我的模型值设置为ngModel.
下面是我的html页面.
<form (ngSubmit)="onSubmit()" #myForm="ngForm">
<div class="form-group">
<label class="control-label" for="id">Employee ID</label>
<input type="text" class="form-control" required [(ngModel)]="model.id" #name="id" #id="ngModel" >
<div [hidden]="id.valid || id.pristine" class="alert alert-danger">
Employee ID is required
</div>
</div>
<div class="form-group">
<label for="name">Employee Name</label>
<input type="text" class="form-control" [(ngModel)]="model.name" name="name" #name="ngModel" required>
<div [hidden]="name.valid || name.pristine" class="alert alert-danger">
Employee ID is required
</div>
</div>
<div class="form-group">
<label for="DOJ">DOJ</label>
<input class="form-control" required [(ngModel)]="model.DOJ" name="DOJ" #DOJ="ngModel" />
<div …Run Code Online (Sandbox Code Playgroud) javascript typescript angular2-forms angular2-template angular
我知道在ng2中我们ComponentFactoryResolver可以解决我们可以申请的工厂ViewContainerRef.
但是,指令有类似之处吗?一种实例化它们并将它们应用于组件中的投影内容的方法?
Can't bind to 'ngModel' since it isn't a known property of 'input'.尽管我已经导入了FormsModule.
包.json:
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "^2.0.0",
Run Code Online (Sandbox Code Playgroud)
在主应用程序模块中:
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
...
@NgModule({
bootstrap: [App],
declarations: [
App
],
imports: [ // import Angular's modules
BrowserModule,
HttpModule,
RouterModule,
FormsModule,
ReactiveFormsModule,
NgaModule,
PagesModule,
routing
]
Run Code Online (Sandbox Code Playgroud)
这是我路由到组件的位置(pages.routes.ts):
import { NewProjectModule, BasicForm } from './new-project/new-project.module';
...
{ path: 'new-project', component: BasicForm },
Run Code Online (Sandbox Code Playgroud)
这是组件模块:
import { FormsModule } from '@angular/forms';
...
import { …Run Code Online (Sandbox Code Playgroud) 有没有办法<style></style>在 Angular2 中动态添加样式表 url 或动态?
例如,如果我的变量是isModalOpenedis true,我想向根组件之外的几个元素添加一些 CSS。喜欢body或html。
可以使用 DOM 或 jQuery 来完成此操作,但我想使用 Angular 2 来完成此操作。
可能的 ?
谢谢
我在 Angular ngFor 循环中有一个选择 HTML 元素:
<select formControlName="type" required>
<option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在 Internet Explorer 中,加载模板时会选择列表中的第一项,但它的值未在表单控件中设置,因此会导致“必需”验证错误,直到在列表中选择另一个值为止。我想要一个具有空值的“请选择”选项,以防止发生这种情况。
这适用于使用或不使用 TypeScript 的 Angular 2+
我有一个放置table位置:input
<tr *ngFor="let persons of ReceivedData">
<td *ngFor="let person of persons">
<div *ngIf="person.personId === editRowId && person.editable === true ">
<input type="number" [(ngModel)]="person.CarCount"
(focusout)="focusOutFunction()"/>
</div>
<div *ngIf="person.editable === false " (click)="toggle(person)">
{{ person.CarCount ? person.CarCount : '-' }}
</div>
</td>
<tr>
Run Code Online (Sandbox Code Playgroud)
但该focusout事件并未被解雇。这是处理该函数的方法focusout:
focusOutFunction() {
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,当未放置在内部时,focusout效果很好:inputtable
<input type="number" (focusout)="focusOutFunction()" />
Run Code Online (Sandbox Code Playgroud)
当我聚焦在桌子内部时如何触发事件?
angular ×10
html ×3
javascript ×3
css ×1
date ×1
input ×1
lost-focus ×1
node.js ×1
projection ×1
typescript ×1