【发布时间】:2018-07-25 20:51:04
【问题描述】:
我是 Angular 4 的新手,使用 MEAN 堆栈开发应用程序。 我的父组件是管理员,子组件是会话。我正在尝试根据子组件值在父组件中设置下拉值。我在插入会话方法中输出相同的发射器事件。但是我无法设置下拉列表的值。请帮忙。
管理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 的选择器一起使用,则会显示整个会话组件
【问题讨论】:
-
this.notify的定义在哪里?我的意思是你在哪里声明它,你在哪里订阅它?
-
@Output() 通知:EventEmitter
= new EventEmitter (); -
我在 session.ts 中添加了这个。但是,这里在代码中漏掉了
-
您在 insertNewSession observable 的“finally”中添加了 this.notify.emit。是否正确调用?你可以在那里添加一些日志记录吗?
标签: html css angular mean-stack