2017-08-14 34 views
0

我想将一个值传递到自举模态。在angular2应用程序。我怎样才能做到这一点?这是我的代码如何在角度2中传递值到自举模态

<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

你如何使用你的模式派生?你只是在Bootstrap中使用它,或者你在Angular代码中创建了一些ModalService/ModalComponent类型的行为?请问 – Lansana

+0

更多信息? –

+0

我正在使用正常的引导程序模型,使用自定义组件 – byteC0de

回答

0

如果你只是想访问索引在0​​你可以通过指定变量来访问它index

<div *ngFor="let item of items; let j = index;"></div>

+0

我想通过该索引到我的模态 – byteC0de

+0

你应该能够通过使用'j'作为参数, PS:我仍然不能看看你在哪里/如何实现你的'* ngFor'。请将其添加到您的代码。 –

1

设置组件属性selectedIndex,并单击打开模式的按钮时,将该值设置为正确的索引。

你缺少代码中的一个ngFor,所以只是假设,在(click)="selectedIndex = i"ii*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(); 
    } 

} 
+0

如何传递多个值 – byteC0de

+0

而不是像我的例子那样传入'[index]',可能会创建一个对象并将整个对象作为'[data]'@Input或其他东西传递。该对象可以包含您的索引和您需要的任何其他信息。 – Lansana