简体   繁体   English

从组件到主组件angular2的调用方法

[英]call method from a component to main component angular2

my issue here is that i have a component that has functions that i want my app.component.html to have access to 我的问题是,我有一个组件,其功能,我希望我的app.component.html有权访问

book.component.html book.component.html

<div class="form-inline">
        <input id="inputSearchBar" class="form-control mr-sm-2" type="text" placeholder="Search" [(ngModel)]="Text">
        <button class="btn btn-outline-info my-2 my-sm-0" type="submit" (click)="onSearch()">Search</button>
    </div>

Now this is currently working because i have this line of code on my book.component.html, what i want is to use this on my app.component.html inside the navbar 现在这个目前正在工作,因为我在book.component.html上有这行代码,我想要的是在导航栏内的app.component.html上使用它

app.component.html app.component.html

<header>
  <nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
    <button class="navbar-toggler navbar-toggler-right" type="button" 
    data-toggle="collapse" 
    aria-controls="navbarColor01"
    aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
    <a class="navbar-brand" href="#">Navbar</a>
    <div class="collapse navbar-collapse" id="navbarColor01" >
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#" [routerLink]="['']">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#" [routerLink]="['about']">About</a>
        </li>
          <li class="nav-item">
          <a class="nav-link" href="#" [routerLink]="['books']">Books</a>
        </li>
         <li class="nav-item">
          <a class="nav-link" href="#" >Register</a>
        </li>
          <li class="nav-item">
          <a class="nav-link" href="#" [routerLink]="['login']">Login</a>
        </li>
      </ul>

    </div>
  </nav>
</header>

I want to have that searchbox as part of my navbar and it being functional, what should i add on my components or what should i do to make it work. 我希望将搜索框作为我的导航栏的一部分并且它正在运行,我应该在我的组件上添加什么或者我该怎么做以使其工作。

you can use parent and child relation ship here. 你可以在这里使用父子关系。 create component for search bar and add in the Parent component. 为搜索栏创建组件并添加父组件。

1) create search component like this 1)像这样创建搜索组件

@Component({
  selector: 'search-component',
})
export class searchcomponent  { }

2) Add in the parent HTML 2)添加父HTML

app.component.html app.component.html

<header>
<search-component>  </search-component>
</header>

More info check this link https://angular.io/docs/ts/latest/cookbook/component-communication.html 更多信息请查看此链接https://angular.io/docs/ts/latest/cookbook/component-communication.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM