我是Angular 4的新手,并使用MEAN堆栈开发应用程序 . 我的父组件是admin,child是session . 我试图根据子组件值设置父组件中的下拉值 . 我在插入会话方法中输出相同的 Launcher 事件 . 但是我无法设置下拉列表的值 . 请帮忙 .
管理员HTML
<div>
<label for="FormType" class="col-sm-2 col-form-label">Select Type </label>
<select #s (change)="setNav(s.value)">
<option *ngFor="let item of dms" >{{item}}</option>
</select>
</div>
<div *ngIf="regTypeSelectedOption==='Session'">
<code for session></div>
<div *ngIf="regTypeSelectedOption==='webinar'">
<code for webinar>
</div>
<div (notify)='onNotify($event)'></div
admin.ts
onNotify(message: string):void{
this.setNav(message);
alert(JSON.stringify(message));
}
setNav(nav:any){
this.selectedNav = nav;
if(this.selectedNav == "Session"){
this.regTypeSelectedOption = "Session";}
else if(this.selectedNav == "Webinar"){
this.regTypeSelectedOption = "Webinar";
}
else if (this.selectedNav == "Select"){
this.regTypeSelectedOption = "Select";
}
}
session.ts
export class SessionComponent implements OnInit {
insertSession(sessionForm){
this.newSession.deliveryMethod = this.regTypeSelectedOption;
this.newSession.formType = "Session";
let updatedresult;
if(this.updatedid == undefined){
this.dataService.insertNewSession(this.newSession)
.subscribe(
res =>{ this.sessions = res;
console.log('response', res)},
err => this.apiError = err,
() => {
this.notify.emit('Session');
this.router.navigate(['/admin']);
}
)
我在div中使用父事件的通知事件 . 如果我使用带有sessioncomponent选择器的notify事件,则会显示整个会话组件
1 回答
我想你想要从一个组件发送消息到另一个组件,而不使用父组件中的子组件 .
我看了你的代码,发现你选择后导航到'/ admin' .
我宁愿建议您在 app.routing.ts 中注册一条路线,如下所示
在 Admincomponent 中使用以下代码从路由中获取数据 .
在 Admincomponent 中实现OnInit并添加以下代码以读取路由值
现在在 ChildComponent 中,替换以下代码:
现在,当插入会话时,它将使用会话ID将您路由回AdminComponent