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