[英]How to access method from .ts file into html file in angular2?
I am beginners for Angular2, I created a modal dialog, here is my HTML file. 我是Angular2的初学者,我创建了一个模式对话框,这是我的HTML文件。 index.html file:-
index.html文件:-
<button type="button" class="btn btn-default" (click)="modal.open()">Open</button> <modal #modal> <modal-header [show-close]="true"> <h4 class="modal-title">I'm a modal!</h4> </modal-header> <modal-body>
Here i want to call show() method from .ts file.
</modal-body>
<modal-footer [show-default-buttons]="true"></modal-footer>
</modal>
After clicking on Open button I want to call show(){....}
that is inside .ts file and I want to display inside <modal-boady></modal-body>
tag. 单击“打开”按钮后,我要调用.ts文件中的
show(){....}
,并且要显示在<modal-boady></modal-body>
标记中。
Here is my hello.component.ts file:- 这是我的hello.component.ts文件:-
import {Component, View} from "angular2/core"; @Component({ selector: 'my-app', templateUrl: './index.html' }) export class HeapMemoryGraphComponent implements OnInit { constructor(){} ngOnInit() { this.show(); } show(){ console.log("=====show()======="); } }
How could I do that? 我该怎么办? please guide me.
请指导我。
Thanks. 谢谢。
There are several ways to get element handle in component. 有几种方法可以在组件中获取元素句柄。 But most understandable guide is here.
但最容易理解的指南在这里。
http://valor-software.com/ngx-bootstrap/#/modals http://valor-software.com/ngx-bootstrap/#/modals
Please check Child Modal there. 请在那里检查儿童模态 。
<button type="button" class="btn btn-primary" (click)="showChildModal()">Open child modal</button>
<div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title pull-left">Child modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="hideChildModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
I am a child modal, opened from parent component!
</div>
</div>
</div>
</div>
import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
@Component({
selector: 'demo-modal-child',
templateUrl: './child.html'
})
export class DemoModalChildComponent {
@ViewChild('childModal') public childModal:ModalDirective;
public showChildModal():void {
this.childModal.show();
}
public hideChildModal():void {
this.childModal.hide();
}
}
As you can see here, component is referenced the handle of modal element in .ts file. 如您所见,.ts文件中的组件被引用为模态元素的句柄。
html file: html文件:
<input class="search-text" type="text" (keyup)="onKeyUp($event)">
<button type="button" [disabled]="!isValidForSearch()" class="btn btn-primary" (click)="getUsers()">Search</button>
ts file: ts文件:
//Here I am getting the value entered in the text box
onKeyUp(event){
this.searchText = event.target.value;
}
//Here I am enabling the button if user enters any text in the search text box other wise it will be disabled
isValidForSearch() {
var isValid = false;
if(this.searchText != '' && this.searchText != undefined) {
isValid = true;
}
return isValid;
}
I hope this helped you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.