我有一个app.component和child.component。我想通过路由器插座中传递的子组件内的变量。如何将值从父组件传递给路由中的子项:Angular 2
路由看起来像这样在app.module.ts:
const routes: Routes = [
{'path': '', 'component': ChildComponent}
]
app.component.html:
<button (click) = "toggle()">toggle</button>
<p>toggle value in app.component.html :</p> {{val}}
<router-outlet></router-outlet>
app.component.ts:
....
....
val = 1;
toggle(){
if(this.val == 1){
this.val = 0;
}else{
this.val = 1;
}
}
....
....
所以,现在我的浏览器输出如下:
现在我想通过这个值1或0的是我得到WHN我按一下按钮,儿童成分,我想表明它“子作品”线是这样的:
单击按钮时,这两个值都应该改变。我试图使用服务,但不工作。我不想在URL中附加val,并将路径作为路由参数发送,因为它将在url中显示。
你可以分享你的服务代码吗?我们可以通过共享服务共享价值 –
@JayakrishnanGounder我对服务感到困惑。如果你能帮助我为这种情况提供服务,那将是非常有帮助的。任何小的代码 –
请找到答案 –