Angular2如何在typescript中引用指令

时间:2016-06-19 16:04:29

标签: angular

我想在我的代码中引用模态,所以我可以在需要时调用show hide

string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
string Query = "Update TopShineDB.Table1 set Time = '" + dr.Cells[0].Text + "', CarColorNumber = '" + dr.Cells[1].Text + "', Interior = '" + dr.Cells[2].Text + "', Exterior = '" + dr.Cells[3].Text + "', CPlastic = '" + dr.Cells[4].Text + "', MPlastic = '" + dr.Cells[5].Text + "', SPlastic = '" + dr.Cells[6].Text + "', PlasticB = '" + dr.Cells[7].Text + "', WashExt = '" + dr.Cells[8].Text + "', WashEng = '" + dr.Cells[9].Text + "', WashTrunk = '" + dr.Cells[10].Text + "', WashSeats = '" + dr.Cells[11].Text + "', SeatsRmv = '" + dr.Cells[12].Text + "', SeatsFit = '" + dr.Cells[13].Text + "', Notes = '" + dr.Cells[14].Text + "', where Time = '" + dr.Cells[0].Text + "' ;";
MySqlConnection conn = new MySqlConnection(constring);
MySqlCommand command = new MySqlCommand(Query, conn);
                                    try
                        {
                            con.Open();
                            command.ExecuteNonQuery();
                            con.Close();
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

模板

import {Component} from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';

// todo: change to ng2-bootstrap
import {MODAL_DIRECTVES, BS_VIEW_PROVIDERS, ModalDirective} from 'ng2-bootstrap/ng2-bootstrap';
// webpack html imports
let template = require('./section.modal.html');

@Component({
  selector: 'section-modal',
  directives: [MODAL_DIRECTVES, CORE_DIRECTIVES],
  viewProviders:[BS_VIEW_PROVIDERS],
  template: template
})
export class SectionModalComponent {
    public lgModal: any;
    show(id){
        console.log(this.lgModal);
        alert("show"+id);
    }
}

我认为我可以使用 #lgModal ,但问题是这个变量只能从模板中看到。

我如何从打字稿中访问它?

1 个答案:

答案 0 :(得分:3)

试试这个:

export class SectionModalComponent {
    @ViewChild('lgModal') lgModal: ElementRef;
    show(id){
        console.log(this.lgModal); //ElementRef
        console.log(this.lgModal.nativeElement); // HTMLElement
        alert("show"+id);
    }
}