- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个连接数组,我使用 angular 2 和 typescript 显示这些连接,如下所示:
<div class="list-item" *ngFor="let con of connectors; let i = index" id="con{{i}}" (click)="connectorSelected(con,i)">
<label class="list-name">{{con.name || "No Available Name"}}</label>
<label class="list-desc">{{con.description}}</label>
</div>
所以基本上当我选择一个连接时,它会在 typescript 中的 typescript 函数 connectorSelected 中突出显示。
private connectorSelected(con: ConnectorModel, index: number) {
this.removeSelectionOnAllFields();
let selectedElement = document.getElementById("con" + index);
selectedElement.className += " selected"
this.conStatus = SaveStatus.UptoDate;
}
private removeSelectionOnAllFields() {
let allClassesHaveSelected = document.getElementsByClassName("selected");
while (allClassesHaveSelected.length) {
allClassesHaveSelected[0].classList.remove("selected");
}
}
但是,当我创建一个新连接时。我向列表中添加一个新连接并尝试突出显示该新创建的连接,但问题是新创建的连接尚未在 HTML5 网页中呈现。这会导致页面抛出错误,因为我们添加到数组的新连接器尚未在 html 页面中创建。因此还没有元素存在,它只是在执行完整个函数后才在网页中创建元素。
新的连接代码:
private newConnector(): void {
let newConenctor: ConnectorModel = new ConnectorModel();
this.connectors.push(newConenctor);
let index: number = this.connectors.length - 1;
this.removeSelectionOnAllFields();
let selectedElement = document.getElementById("con" + index);
selectedElement.className += " selected"
}
我想知道我应该如何处理这个问题?
最佳答案
这是因为 Angular 不触发变化检测。
为了解决这个问题,你应该复制你的数组。
this.connectors.push(newConenctor);
this.connectors = this.connectors.slice(0);
否则就像我在评论中所说的那样,我建议您使用指令 [ngClass],它更简单,您不必使用全局文档。
您可以在此处找到文档:https://angular.io/docs/ts/latest/api/common/index/NgClass-directive.html
关于html - Angular 2 在动态生成的 "list"中向新元素添加高亮显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44056154/
我正在用 Java 编写代码,并且使用 Vaadin 8 扩展。 我有一个 Vaadin 组合盒,效果很好。但我不仅想从组合框中选择项目,还想选择书面输入。这意味着我想使用组合框作为下拉菜单以及文本编
我正在尝试将 AJAX 添加到 JQuery ListView 中并呈黄色闪烁,但我似乎无法使其正常工作。谁能指出我正确的方向? http://jsfiddle.net/zFybm/ 最佳答案 根据
我有这个样式表: .pixel{ position: absolute; height: 10px; width: 10px; background-color: #f
这是我用来将新行推送到容器的一行代码: this.$el.append(new ItemView(item).render().el); 其中item是Backbone.js model,render
我正在尝试在 anguar.js 中制作一些测试应用程序,但遇到了问题。我的 js 文件包含: live = angular.module('live',[]); live.controller('p
如何绑定(bind)页面加载后创建的新元素? 我有这样的东西 system = function() { this.hello = function() { alert
html5 新元素(页眉、导航、页脚等)在 IE 中不工作 最佳答案 您需要包含 HTML5 shiv 脚本以允许在旧版 IE 浏览器中设置 HTML5 元素的样式:http://code.googl
我是一名优秀的程序员,十分优秀!