0
A
回答
-1
這裏是如何實現這種方式。
MainComponent.ts
import { Component } from '@angular/core';
import { MyService1 } from '../../providers/myService1/myService1';
import { MyCmp1 } from '../../components/myCmp1/myCmp1';
@Component({
templateUrl: 'build/pages/mainComponent/mainComponent.html',
providers: [
MyService1
],
directives: [
MyCmp1
],
//pipes: [ ... ] // Pipes which you are using
})
export class MainComponent {
main: any;
constructor() {
this.load();
}
load(): void {
MyService1.loadData()
.then((response) => {
main.data1 = response;
});
}
onShowMe(data): void {
alert(data);
}
}
MainComponent.html
<div class="main-component">
<h1>Main Component</h1>
<my-cmp-1 [data1]="main?.data1" (onShowMe)="onShowMe($event)"></my-cmp-1>
</div>
MyCmp1.ts
import { Component, EventEmitter } from '@angular/core';
import { Input, Output } from '@angular/core';
@Component({
selector: 'my-cmp-1',
templateUrl: 'build/components/myCmp1/myCmp1.html'
})
export class MyCmp1 {
@Input() data1: any;
@Output() onShowMe = new EventEmitter();
showMe(event: Event, data: any): void {
event.preventDefault(); // If you are using <a> or etc.
this.onShowMe.emit(data);
}
}
MyCmp1.html
<h3>My Component 1</h3>
<a href="" (click)="showMe($event, 'message from MyCmp1')">Show Me</a>
相關問題
- 1. Angular2創建組件
- 2. 如何從Angular2中的其他組件調用組件中的函數?
- 3. 從其他組件的事件創建組件
- 4. 如何根據React JS中的其他組件創建Dropdown組件?
- 5. React Native - 如何在其他組件上創建和渲染組件
- 6. 動態地angular2任何事件在其他組件
- 7. 如何在React中創建Toast等全局組件並將其引用到其他組件中?
- 8. 如何在其他組件中使用家庭組件數據?
- 9. Angular2 Reactive Form - 其他組件中的表單控件
- 10. 如何在Angular2中動態創建SVG組件?
- 11. 動態創建ng-content的angular2組件
- 12. cycle.js - 嵌入組件在其他組件
- 13. 如何使用HttpResponse或其他內容創建組件的動態http響應
- 14. Angular2 @ViewChildren在其他組件中返回undefined
- 15. Angular2 CLI Angular-Meteor組件創建
- 16. Angular2:動態組件創建:AOT編譯
- 17. 如何在angular2中更新父組件的父組件
- 18. 基於URL路由加載其他組件內的子組件
- 19. 創建循環發送變量到Flex中的其他組件
- 20. Angular2創建一個顯示外部網頁內容的組件
- 21. 如何在Joomla 1.5中創建組件?
- 22. 如何在Tapestry 5中創建組件?
- 23. 如何在joomla1.6中創建新組件?
- 24. 如何在javascript中創建html組件
- 25. 如何在Swing中創建此組件?
- 26. 如何讓組件檢測其在angular2中的隱藏狀態?
- 27. 如何約束包含其他組件的Web組件?
- 28. 如何在同一行中存在其他組件時水平居中組件?
- 29. 如何在Angular2中引用組件?
- 30. Angular2 - 如何動態創建組件並追加到body的viewcontainer