Angular 2에서 클릭 이벤트에 대한 함수 호출
컴포넌트 (typescript) 내부에서 함수를 선언하고 Angular 2의 클릭 이벤트에서 호출하는 방법은 무엇입니까? 다음은 Angular 2 코드가 필요한 Angular 1의 동일한 기능에 대한 코드입니다.
<button ng-click="myFunc()"></button>
//제어 장치
app.controller('myCtrl', ['$scope', function($cope) {
$scope.myFunc= {
console.log("function called");
};
}]);
구성품 코드 :
import { Component } from "@angular/core";
@Component({
templateUrl:"home.html"
})
export class HomePage {
public items: Array<string>;
constructor() {
this.items = ["item1", "item2", "item3"]
}
public open(event, item) {
alert('Open ' + item);
}
}
전망:
<ion-header>
<ion-navbar primary>
<ion-title>
<span>My App</span>
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let item of items" (click)="open($event, item)">
{{ item }}
</ion-item>
</ion-list>
</ion-content>
코드에서 볼 수 있듯이 이와 같이 클릭 처리기를 선언하고 (click)="open($event, item)"
이벤트와 항목 (에서 선언 됨 *ngFor
)을 open()
메서드 (구성 요소 코드에서 선언 됨 )로 보냅니다 .
항목 만 표시하고 이벤트에서 정보를 얻을 필요가없는 경우 다음 (click)="open(item)"
과 같이 open
메서드를 수행 하고 수정할 수 있습니다.public open(item) { ... }
Angular2 + 로의 정확한 전송 은 다음과 같습니다.
<button (click)="myFunc()"></button>
또한 구성 요소 파일에서 :
import { Component, OnInit } from "@angular/core";
@Component({
templateUrl:"button.html" //this is the component which has the above button html
})
export class App implements OnInit{
constructor(){}
ngOnInit(){
}
myFunc(){
console.log("function called");
}
}
https://angular.io/guide/user-input- 간단한 예가 있습니다.
The line in your controller code, which reads $scope.myFunc={
should be $scope.myFunc = function() {
the function()
part is important to indicate, it is a function!
The updated controller code would be
app.controller('myCtrl',['$scope',function($cope){
$scope.myFunc = function() {
console.log("function called");
};
}]);
This worked for me: :)
<button (click)="updatePendingApprovals(''+pendingApproval.personId, ''+pendingApproval.personId)">Approve</button>
updatePendingApprovals(planId: string, participantId: string) : void {
alert('PlanId:' + planId + ' ParticipantId:' + participantId);
}
참고URL : https://stackoverflow.com/questions/40211730/call-a-function-on-click-event-in-angular-2
'developer tip' 카테고리의 다른 글
jinja2 템플릿에서 jinja2 구문 이스케이프 (0) | 2020.11.17 |
---|---|
Microsoft Edge가 일부 로컬 웹 사이트를 열지 만 다른 웹 사이트는 열지 않는 이유는 도메인 이름이 호스트 파일에서 127.0.0.1로 라우팅되는 이유 (0) | 2020.11.17 |
파이썬으로 모듈을 어떻게 문서화합니까? (0) | 2020.11.17 |
WPF ListView 항목을 가로 스크롤 막대처럼 가로로 반복하려면 어떻게해야합니까? (0) | 2020.11.16 |
WPF 전역 글꼴 크기 (0) | 2020.11.16 |