我想将一个值传递给我的bootstrap模式。在angular2应用程序中。我怎么能实现这个目标?这是我的代码
<button href="" class="ui red button"
[attr.data-index]="i"
data-toggle="modal"
data-target="#deleteVersion"
data-backdrop="static">Delete</button>
<confirm-dialog [id]="'deleteVersion'" [title]="'Delete Version'" [content]="'Do you want to delete this version ?'" [positiveCall]="deleteVersion.bind(this)"></confirm-dialog>
@Component({
selector: 'confirm-dialog',
templateUrl: './confirm-dialog.component.html',
styleUrls: ['./confirm-dialog.component.css']
})
export class ConfirmDialogComponent implements OnInit {
@Input()
id: String;
@Input()
title: String;
@Input()
content: String;
@Input()
positiveCall: Function;
constructor() { }
ngOnInit() {
}
yes() {
this.positiveCall = this.positiveCall || function () { };
this.positiveCall();
}
}
我想在ngFor
中传递索引,提前致谢
答案 0 :(得分:1)
设置组件属性selectedIndex
,然后单击打开模式的按钮,将该值设置为正确的索引。
您的代码中缺少ngFor
,因此请假设i
中的(click)="selectedIndex = i"
来自i
中的*ngFor="let item of items; let i = index"
<button href="" class="ui red button"
data-toggle="modal"
data-target="#deleteVersion"
data-backdrop="static"
(click)="selectedIndex = i">Delete</button>
<confirm-dialog [index]="selectedIndex" [id]="'deleteVersion'" [title]="'Delete Version'" [content]="'Do you want to delete this version ?'" [positiveCall]="deleteVersion.bind(this)"></confirm-dialog>
@Component({
selector: 'confirm-dialog',
templateUrl: './confirm-dialog.component.html',
styleUrls: ['./confirm-dialog.component.css']
})
export class ConfirmDialogComponent implements OnInit {
@Input()
id: String;
@Input()
title: String;
@Input()
content: String;
@Input()
positiveCall: Function;
selectedIndex: number;
constructor() { }
ngOnInit() {
}
yes() {
this.positiveCall = this.positiveCall || function () { };
this.positiveCall();
}
}
答案 1 :(得分:0)
如果您只想访问index
中的索引,只需将变量分配给<div *ngFor="let item of items; let j = index;"></div>
{{1}}