如何在Angular中将输入值作为参数传递给路由器

Asalas77

我相信我要尝试的工作是微不足道的,但是我尝试了许多不同的事情,无法解决。

我有两个部分,SearchComponent并且DetailsComponent显示搜索结果。

路由模块:

const routes: Routes = [
{ path: '', component: SearchComponent},
{ path: 'armory/:name', component: DetailsComponent}
];

@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})

export class AppRoutingModule {}

和输入 search.component.html

<form class="form-inline container-fluid">
  <input type="text" id="name" placeholder="Character name..." class="form-control">
  <button (click)="onSearch( ... <!--name should be here-->)" class="btn">
    <span class="fa fa-search"></span></button>
</form>

然后在search.component.ts

export class SearchComponent implements OnInit {

constructor(private router: Router) {
}

onSearch(name: string) {
   this.router.navigate(['/armory', name])
}
}

如何获取name输入的值并将其作为参数传递给onSearch()

亨利·戴夫

如果要获取name的值,您可以像这样简单地为输入字段分配一个引用#name这是您需要做的:

search.component.html

 <form class="form-inline container-fluid">
  <input type="text" id="name" #name  placeholder="Character name..." 
  class="form-control">
  <button (click)="onSearch(name.value)" class="btn">
   <span class="fa fa-search"></span></button>
  </form>

希望能帮助到你!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在方法函数中将参数传递给vue.js路由器?

如何在 API 调用中将输入值作为参数传递?

将参数传递给Angular UI路由器模板

如何在Golang中将vars传递给chi路由器

如何在VIPER设计模式中将ViewController参考传递给路由器?

如何在打字稿中将服务实例传递给反应路由器组件

如何将具有相同查询参数的值的数组传递给路由器?

如何在Laravel中将路由参数传递给webcontroller?

如何在Java编辑器中将文本作为参数传递给我的netbeans模块?

如何在Golang路由中将URL作为参数传递?

如何在C#中将组合框值作为参数传递给Crystal Report

如何在SSRS中将字段的所有值作为参数传递给子报表?

如何在Java中将函数作为参数传递给Callable

如何在sapply中将公式作为参数传递给lm?

如何在 Django 中将 id 作为参数传递给视图?

如何在Python中将列表作为参数传递给函数

如何在反应路由器v6中将路径url中的id作为组件属性传递

如何在Oracle中将逗号分隔的值列表作为参数输入传递

参数未传递给服务器路由器

如何修复传递给 Angular 路由参数的值?

通过路由器将输入传递给Angular组件

如何在Vuejs中将道具传递给输入值

如何在$ routeProvider中将参数传递给控制器?

如何在Flutter中将参数传递给侦听器?

如何在angularjs中将参数传递给控制器?

如何在脚本中将值传递给参数

将路由器参数传递给组件对象

如何在Angular中将参数传递给模态

如何在Angular指令中将参数传递给函数?