如何将值传递给角度2中的bootstrap模态

时间:2017-08-14 12:02:14

标签: twitter-bootstrap angular typescript

我想将一个值传递给我的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中传递索引,提前致谢

2 个答案:

答案 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}}