Arrays 角度2我不知道如何删除推送对象的一行
首先,让我澄清我的问题: 首先,它是这样的:在我的例子中,它删除了最后创建的对象 但我想单独删除每个条件,如下所示,请参见每行旁边的红色框: 在上面的代码中,我将在我的案例中导入带有条件对象的getConditions。有人知道我是怎么做的吗?处理这个问题的最佳方法是什么 我想在这里Arrays 角度2我不知道如何删除推送对象的一行,arrays,typescript,angular,Arrays,Typescript,Angular,首先,让我澄清我的问题: 首先,它是这样的:在我的例子中,它删除了最后创建的对象 但我想单独删除每个条件,如下所示,请参见每行旁边的红色框: 在上面的代码中,我将在我的案例中导入带有条件对象的getConditions。有人知道我是怎么做的吗?处理这个问题的最佳方法是什么 我想在这里 import {Component, OnInit, Input, DynamicComponentLoader, ElementRef} from 'angular2/core'; import {Condit
import {Component, OnInit, Input, DynamicComponentLoader, ElementRef} from 'angular2/core';
import {Condition} from './condition';
import {DateCondition} from './datecondition.component';
import {StringCondition} from './stringcondition.component';
import {SelectCondition} from './selectcondition.component';
import {ConditionBuilderComponent} from "./conditionbuilder.component";
@Component({
selector: 'condition-detail',
template: '<div #child></div>'
+ '<a class="btn btn-danger" (click)="deleteCondition()"><i class="glyphicon glyphicon-minus"></i></a>'
})
export class ConditionDetailComponent implements OnInit {
@Input() condition: Condition;
dcl:DynamicComponentLoader;
elementRef:ElementRef;
constructor(dcl: DynamicComponentLoader, elementRef: ElementRef) {
this.dcl = dcl;
this.elementRef = elementRef;
}
ngOnInit() {
this.dcl.loadIntoLocation(this.condition, this.elementRef, 'child');
}
deleteCondition() {
HOW CAN I DELETE THE CONDITION ROW HERE?
}
就像这是代码构建一样,我希望你能清楚地帮助我。deleteCondition方法如何知道需要删除哪一行,以及如何从页面上方代码的数组中删除该行
我希望有人能帮助我 您可以向子组件提供列表,并从中删除当前元素。这样,将删除循环中的关联组件 主要组成部分:
@Component({
selector: 'my-app',
template: `
<div *ngFor="#elt of data">
<my-component [elt]="elt" [data]="data"></my-component>
</div>
`,
directives: [MyComponent]
})
export class AppComponent {
constructor() {
this.data = [
{name: 'name1'},
{name: 'name2'},
{name: 'name3'},
{name: 'name4'}
];
}
}
@Component({
selector: 'my-component',
template: `
<div>{{elt.name}} <span (click)="delete()">X</span></div>
`
})
export class MyComponent {
@Input()
elt: any;
@Input()
data: any[];
delete() {
var index = this.data.indexOf(this.elt);
this.data.splice(index, 1);
}
}
子组件:
@Component({
selector: 'my-app',
template: `
<div *ngFor="#elt of data">
<my-component [elt]="elt" [data]="data"></my-component>
</div>
`,
directives: [MyComponent]
})
export class AppComponent {
constructor() {
this.data = [
{name: 'name1'},
{name: 'name2'},
{name: 'name3'},
{name: 'name4'}
];
}
}
@Component({
selector: 'my-component',
template: `
<div>{{elt.name}} <span (click)="delete()">X</span></div>
`
})
export class MyComponent {
@Input()
elt: any;
@Input()
data: any[];
delete() {
var index = this.data.indexOf(this.elt);
this.data.splice(index, 1);
}
}
请看这个plunkr:。所以我将通过输入传递数组的值。我认为这不是最好的方法。您可以使用EventEmitter通知父级删除。