【问题标题】:Angular2/5 search a database and display results on new pageAngular2/5 搜索数据库并在新页面上显示结果
【发布时间】:2018-02-27 02:18:19
【问题描述】:

我已经用 express 设置了 MLABS,它可以很好地连接到数据库,然后我有一个 angular2 服务,当单击按钮为 MLAB 执行 GET 时,它可以联系 expressjs。这是我感到困惑的部分,当单击按钮时,希望 angular2 移动到新组件(页面)以显示搜索结果并获取 GET 的结果并在那里显示它们。按钮和 GET 由充当导航栏的 app.component 完成,因此我在应用程序中的位置无关紧要,我应该能够在顶部的搜索栏中输入字符串,它会将我带到一个页面单击导航栏上的搜索图标后的结果。

我在应用顶部的搜索栏:

<div class="col-sm-3 col-md-3">
              <form class="navbar-form" role="search">
              <div class="input-group">
                  <input type="text" class="form-control" placeholder="Search" name="q">
                  <div class="input-group-btn">
                      <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
                  </div>
              </div>
              </form>
          </div>

路由模型:

const appRoutes: Routes = [
{
    path: '',
    component: WelcomeComponent
},
{
    path:'home',
    component: HomeComponent
},
{
    path:'profile',
    component: ProfileComponent
},
{
    path: 'request',
    component: RequestComponent
},
{
    path: 'mapsmenu',
    component: MapsMenuComponent
},
{
    path: 'activity',
    component: TransactionsComponent
},
{
    path: 'poststatus',
    component: PostStatusComponent
},
{
    path: 'map',
    component: CryptoMapComponent
},
{
    path: 'trading',
    component: tradingComponent
},
{
    path: 'friends',
    component: FriendsComponent
},
{
    path: 'linkwallet',
    component: WalletComponent
},
{
    path: 'newwallet',
    component: NewWalletComponent
},
{
    path: 'settings',
    component: SettingsComponent
},
{
    path: 'register',
    component: RegisterComponent
},
{
    path: 'FAQ',
    component: FAQComponent
}
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

【问题讨论】:

  • 你能发布你的路由模块吗?
  • 我在那里添加的

标签: angular mean-stack mlab


【解决方案1】:

创建一个SearchResultComponent并将其添加到路由模块:

constructor(private route:ActivatedRoute){}
ngOnInit(){
    this.route.queryParams.debounceTime(500).distinctUntilChanged().subscribe(params =>{
      // perform search here and bind result to template only after the input has changed and 500ms have passed
    })
}

在您的搜索栏中:

<input #search type="text" class="form-control" placeholder="Search" name="q">
<button class="btn btn-default" type="submit" (click)="goToSearch(search.value)"><i class="glyphicon glyphicon-search"></i></button>

在您的搜索栏组件中

goToSearch(search:string){
    this.router.navigateByUrl(`/search?${search}`)
}

【讨论】:

  • @Safiyya 不错,但最好添加 .debounceTime(500).distinctUntilChanged(),这样我们就不会在每个符号输入 `this.route.queryParams.debounceTime(500).distinctUntilChanged 时发送请求().subscribe(params => { // 在这里执行搜索并将结果绑定到模板 })`
  • 说“MlabsSearchComponent”类型上不存在属性“路由”,我该如何声明路由?
  • 将其添加为 publicprivate 属性(更新答案)。
  • 试过它说 [ts] 参数属性只允许在构造函数实现中使用。 (参数)路由:ActivatedRoute
  • 哈是constructor 不是contructor !!
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 2016-04-07
  • 2013-12-15
  • 1970-01-01
  • 2021-08-13
  • 2011-08-25
  • 2017-06-21
相关资源
最近更新 更多