【发布时间】:2019-05-10 10:26:25
【问题描述】:
我必须将组件导航栏和表单-diyalog 分开。我想在导航栏中使用 form-diyalog 中的值。
这是我的 navbar.ts
import { Component, OnInit } from "@angular/core";
import { MenuItemModels } from "./models/MenuItemModel";
@Component({
selector: "app-navbar",
templateUrl: "./navbar.component.html",
styleUrls: ["./navbar.component.css"]
})
export class NavbarComponent implements OnInit {
MenuItem: MenuItemModels[] = [
{ name: "Anasayfam", link: "#" },
{ name: "Hakkımızda", link: "#" },
{ name: "Üniversitemiz", link: "#" },
{ name: "Fakültelerimiz", link: "#" },
{ name: "Bize Ulaşın", link: "#" }
];
addItem() {
let customObj = new MenuItemModels();
customObj.name = customObj.link = "#";
this.MenuItem.push(customObj);
}
deleteItem(i) {
this.MenuItem.splice(i, 1);
}
constructor() {}
ngOnInit() {}
}
这是我的表单-diyalog.ts
import { Component, OnInit, Input, Output } from "@angular/core";
import { FormControl } from "@angular/forms";
@Component({
selector: "app-form-diyalog",
templateUrl: "./form-diyalog.component.html",
styleUrls: ["./form-diyalog.component.css"]
})
export class FormDiyalogComponent implements OnInit {
name = new FormControl("");
constructor() {}
ngOnInit() {}
}
这是 form-diyalog.html
<label>
Menu Name:
<input type="text" [formControl]="name">
</label>
<p>
Value: {{ name.value }}
</p>
我可以在 form-diyalog 组件中写入 name.value,但不能在导航栏组件中写入。我怎样才能做到这一点?我试过@output,@input,但我做不到。
【问题讨论】:
-
使用
Subject怎么样?基本示例:stackblitz.com/edit/… -
您是否在导航栏中使用此标签?
-
没用
标签: angular angular-material angular-cli