Angular 将值传递给不同组件中的函数
我有两个组件。一个是页面,另一个是在页面上滑动的过渡。。我希望向transitionComponent中的函数传递一个值,但我想不通。。@input是实现这一点的最佳方法,还是有其他方法可以直接调用另一个组件中的函数 使用代码示例编辑-我精简了主要组件,只关注传递此值: 父组件:Angular 将值传递给不同组件中的函数,angular,angular2-template,angular2-directives,Angular,Angular2 Template,Angular2 Directives,我有两个组件。一个是页面,另一个是在页面上滑动的过渡。。我希望向transitionComponent中的函数传递一个值,但我想不通。。@input是实现这一点的最佳方法,还是有其他方法可以直接调用另一个组件中的函数 使用代码示例编辑-我精简了主要组件,只关注传递此值: 父组件: import {Component, ElementRef, OnInit} from '@angular/core'; import {TransitionCompon
import {Component, ElementRef, OnInit} from '@angular/core';
import {TransitionComponent} from '../../PageLoader/TransitionComponent';
@Component({
selector: 'home',
templateUrl: './app/components/Homepage/list/index.html',
directives: [ TransitionComponent]
})
export class HomepageListComponent implements OnInit {
transitionState: string;
constructor() {
this.transitionState = this.transitionState;
}
ngOnInit() {
this.transitionState = "test";
}
}
子组件:
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'the-loader',
template: `<div class="loader"></div>`,
styles: [`
.loader {
background-color: black;
height: 100%;
width:100%;
position:absolute;
top:0;
left:0;
z-index:99999;
}
`]
})
export class TransitionComponent {
@Input() transitionState;
transitionStatus(transitionState) {
alert(transitionState);
}
}
从'@angular/core'导入{Component,Input,OnInit};
@组成部分({
选择器:“加载程序”,
模板:``,
风格:[`
.加载器{
背景色:黑色;
身高:100%;
宽度:100%;
位置:绝对位置;
排名:0;
左:0;
z指数:99999;
}
`]
})
导出类转换组件{
@Input()转换状态;
过渡状态(过渡状态){
警报(转换状态);
}
}
parent=>child
和child=>parent
交换数据。。。
服务(sharedService)
@ViewChild API
。注意:您不需要使用@Input 与RC-
parent=>child
和child=>parent
交换数据。。。
服务(sharedService)
@ViewChild API
。注意:您不需要使用@Input 与RC-
在父子关系中,您可以使用: ViewChild:当父组件需要访问其子组件的成员时,例如,您可以从父组件调用子组件的函数 @输入:允许您将值从父对象传递给子对象
在您的情况下,我会在父子关系中使用@input aprouch,您可以使用: ViewChild:当父组件需要访问其子组件的成员时,例如,您可以从父组件调用子组件的函数 @输入:允许您将值从父对象传递给子对象
在您的情况下,我会使用@input aprouch传递字符串时,我似乎无法理解这一点,我已更新了上面的问题。如果你能看一下并给我一些建议,我将不胜感激。因为传递字符串,我似乎无法理解这一点,我已经更新了上面的问题。如果你能看一下,给我一些非常有用的信息,我会非常感激,因为我还没有使用viewchild、eventemitter或injector。我在上面添加了一些代码示例。很高兴能得到你的想法,因为我似乎无法理解如何使用@inputHanks传递字符串。它看起来很棒,我使用的是RC1,它现在给了我这个错误。类型错误:无法读取未定义的属性“transitionStatus”,我的答案与RC重复。还有一件事,如果我想访问函数在html模板中传递的值,我该怎么做。。我正在尝试这个,但它没有输出它的模板:
值是{{VALUE}
,transitionStatus(VALUE){this.someVar=VALUE}代码>在html中放入{{someVar}}
。就是这样。这是很好的信息,还没有使用viewchild、eventemitter或injector。我在上面添加了一些代码示例。很高兴能得到你的想法,因为我似乎无法理解如何使用@inputHanks传递字符串。它看起来很棒,我使用的是RC1,它现在给了我这个错误。类型错误:无法读取未定义的属性“transitionStatus”,我的答案与RC重复。还有一件事,如果我想访问函数在html模板中传递的值,我该怎么做。。我正在尝试这个,但它没有输出它的模板:值是{{VALUE}
,transitionStatus(VALUE){this.someVar=VALUE}代码>在html中放入{{someVar}}
。就这样。
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; // <---ViewChild added
import {TransitionComponent} from '../../PageLoader/TransitionComponent';
@Component({
selector: 'home',
templateUrl: './app/components/Homepage/list/index.html',
directives: [ TransitionComponent]
})
export class HomepageListComponent{
@ViewChild(TransitionComponent) vc:TransitionComponent;
ngAfterViewInit()
{
this.vc.transitionStatus('Angular2');
}
}
export class TransitionComponent {
transitionStatus(transitionState:string) {
alert(transitionState);
}
}