- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的 Angular 2 应用程序中,我想要一个输入列表。在其中一个中按 Enter 键将添加一个新的输入并立即关注它。这是一个已经在本网站和 Eric Martinez provided a neat answer to it that accomplishes that with a custom directive 上提出的问题.
他的解决方案基于一个虚拟的整数列表。我在尝试使其适应更现实的场景时遇到困难。我已经 fork 了 Eric 的 plunk,所以你可以 run the code here , 但最重要的文件是这个:
//our root app component
import {Component, Directive, Renderer, ElementRef} from 'angular2/core'
class Person { name: string }
@Directive({
selector : 'input'
})
class MyInput {
constructor(public renderer: Renderer, public elementRef: ElementRef) {}
// It won't work at construction time
ngOnInit() {
this.renderer.invokeElementMethod(
this.elementRef.nativeElement, 'focus', []);
}
}
@Component({
selector: 'my-app',
providers: [],
template: `
<div *ngFor="#input of inputs">
<input
(keydown.enter)="add()"
[(ngModel)]="input.name"
type="text"/>
</div>
`,
directives: [MyInput]
})
export class App {
inputs: Person[] = [{name: 'Alice'}];
add() {
var newPerson = new Person();
newPerson.name = 'Bob';
this.inputs.push(newPerson);
}
}
我的数组 inputs
现在是 Person
的列表对象。输入双向绑定(bind)到 name
Person
的属性. <input>
现在包裹在 <div>
中,正如我预期的那样,稍后我会编写更多标记来显示每个 Person
.
进行这些更改后,该示例仅在第一次尝试按 Enter 时有效 - 带有文本 Bob 的新输入按预期出现。但是当我第二次尝试按 Enter 时,出现错误:
angular2.dev.js:23730 Error: Expression 'ngClassUntouched in App@2:6' has changed after it was checked. Previous value: 'true'. Current value: 'false'
at ExpressionChangedAfterItHasBeenCheckedException.BaseException [as constructor] (angular2.dev.js:7587)
at new ExpressionChangedAfterItHasBeenCheckedException (angular2.dev.js:4992)
at ChangeDetector_App_1.AbstractChangeDetector.throwOnChangeError (angular2.dev.js:9989)
at ChangeDetector_App_1.detectChangesInRecordsInternal (viewFactory_App:143)
at ChangeDetector_App_1.AbstractChangeDetector.detectChangesInRecords (angular2.dev.js:9874)
at ChangeDetector_App_1.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9857)
at ChangeDetector_App_0.AbstractChangeDetector._detectChangesContentChildren (angular2.dev.js:9930)
at ChangeDetector_App_0.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9858)
at ChangeDetector_HostApp_0.AbstractChangeDetector._detectChangesInViewChildren (angular2.dev.js:9936)
at ChangeDetector_HostApp_0.AbstractChangeDetector.runDetectChanges (angular2.dev.js:9861)
我该如何解决?
我在 Chrome 中运行示例。我发现使用基于 Beta 12 版 Angular2 的 Eric Martinez 的插件来演示问题最简单,但我在现实世界中遇到相同错误的应用程序目前使用的是 Angular 2.0.0。
最佳答案
Angular2 不喜欢在更改检测回调期间更改模型(就像 ngOnInit()
那样)。调用 ChangeDetectorRef.detectChanges()
应该可以修复它:
class MyInput {
constructor(public renderer: Renderer, public elementRef: ElementRef
,private cdRef:ChangeDetectorRef) {}
// It won't work at construction time
ngOnInit() {
this.renderer.invokeElementMethod(
this.elementRef.nativeElement, 'focus', []);
this.cdRef.detectChanges();
}
}
关于 Angular 2 : focusing input causes 'expression has changed after it was changed' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562845/
我的 Web 应用程序在后端使用 Node.js 和 Express。当违反内容安全策略 (CSP) 时,报告 URI 报告空对象。我的后台代码如下: app.use(bodyParser.urlen
在服务器端提供静态服务的方式在 Express 中似乎非常简单: To serve static files such as images, CSS files, and JavaScript fil
var express = require('express'); var app = express(); 这就是我们创建快速应用程序的方式。但是这个'express()'是什么?它是方法还是构造函
我在尝试安装时收到以下错误 express : npm ERR! code ERR_OSSL_PEM_NO_START_LINE npm ERR! errno ERR_OSSL_PEM_NO_STAR
如 express 所述routing guide和 this answer ,我们可以创建“迷你应用程序”并从主应用程序使用它。但是我看到一段代码,它在模块中使用 app 而不是 router ap
我正在写一个 NestJS应用。现在我想安装 Express中间件 express-openapi-validator . 但是,我无法让它工作。有一个 description for how to
我看过很多类似的帖子,似乎我声明的 var1 似乎需要在其他地方传递,但我似乎无法弄清楚。 public Expression> CreateEqualNameExpression(string ma
Express(或 Connect 的)bodyParser 中间件被标记为已弃用,建议用户改用: app.use(connect.urlencoded()) app.use(connect.json
我只是想知道这种看似尴尬的配置的原因是什么(来自 Getting Started w/ Apollo Server ), const server = new ApolloServer({ //
我正在尝试在表单组中写入表单控件特定的验证错误消息。我在网上找到了几个教程和示例 ( such as this one ),概述了一个看似简单的 *ngIf div,如果在控件上检测到错误,则显示错误
我有一个简单的 Express 应用程序,托管在 AWS 上,使用无服务器框架。 我正在使用 serverless-http 包装 express 应用程序以部署到 AWS lambda 函数,并使用
我最近在 mozilla 教程的帮助下安装了 node 和 express。我正在安装应用程序生成器的下一步,但是当我运行时 npm install express-generator -g 在我的终
我遇到过两种不同的方式来定义 express、use() 中间件,我想知道它们之间是否有任何区别,或者它是否只是语法糖? 一个 const app = express(); app.use(cors(
我试图让我的 Jade 模板编写一个相对于当前 URL 的超链接 ( )。 例如,我的 View 是从 http://localhost/cats 调用的它看起来像这样: extends layou
检查 Express 文档我在下面看到了这种解决方案: app.all('/*', function(req, res) { console.log('Intercepting request
我似乎无法弄清楚如何包含多个模型。 我有三个模型。Tabs, Servers, and PointsTabs hasMany ServerServers belongsTo Tabs and hasM
我已使用Web PI安装IIS Express。在托盘中,没有IIS Express图标。如何在不使用命令行的情况下启动IIS Express?我希望IIS永久运行,因此没有命令行。 最佳答案 参见R
我不想在我的网站上使用 Jade 或 EJS。如何在不默认使用 Jade 模板的情况下创建快速站点?谢谢 最佳答案 如果您想要的是直接为静态 html 文件提供缓存资源的可能性,同时仍然能够点击“/”
Express是否支持HTTP动词“PATCH”,例如: app.patch("/api/resource", function(req, res){ ... }); 我检查了文档,对我来说似乎还不清
我正在快速服务器中运行 vue SPA。问题是当使用历史模式并刷新页面时,我得到一个 404 not found 异常。我尝试使用 connect-history-api-fallback 但不起作用
我是一名优秀的程序员,十分优秀!