简体   繁体   English

将输入从component.html发送到component.ts

[英]Send input from component.html to component.ts

I'm doing a search, and I'm receiving the value I need to search from the User on my component.html . 我正在搜索,并且正在我的component.html上从用户那里接收需要搜索的值。

I'm sending the data to the component.ts like the code below. 我正在将数据发送到component.ts就像下面的代码一样。

<input class="form-control mr-sm-2" #query (keyup.enter)="search(query.value)">
<a class="btn btn-light my-2 my-sm-0" (click)="search(query.value)">Buscar</a>

And in my component.ts I've the following code 在我的component.ts我有以下代码

search(query: string) {
    if (query !== '') {
        window.location.href = 'http://mydomain/result.html?name=' + query;
    }
}

My doubt is, why the window.location.href always change the current URL in the browser on Firefox Developer Edition , and only sometimes on Google Chrome and Firefox Quantum . 我的疑问是,为什么window.location.href 总是Firefox Developer Edition上的浏览器中总是更改 current URL ,而有时仅在Google ChromeFirefox Quantum上才更改

If I click on the button , It always work as expected, but if I hit the enter , It work only sometimes on the other 2 browsers, and I don't know why this is happening, can you guys help me with that? 如果单击按钮 ,它将始终按预期运行,但是如果按回车 ,则在其他两个浏览器上才能运行 ,并且我不知道为什么会发生这种情况,你们可以帮我吗?

  • EDIT 编辑

I've noticed while debugging that, when I hit enter, it doesn't call the function search . 我在调试时注意到,当我按Enter键时,它不会调用函数search

You are using (keyup.enter) when in reality you want (key.enter) . 实际上,您在使用(keyup.enter)时就在使用(key.enter) Also, you don't need a reference #query . 另外,您不需要引用#query You could write input as: 您可以将输入编写为:

<input class="form-control mr-sm-2" (key.enter)="search($event)">

In the method use: 在方法中使用:

search(event: Event) {
    const query = (event.target as HTMLInputElement).value;

    if (query !== '') {
        window.location.href = 'http://mydomain/result.html?name=' + query;
    }
}

It seems that FireFox is catching keyup and not enter . 看来FireFox正在抓紧keyup而不enter

I've found a solution, I've changed the <input> tag content and the tag <a> to <button> , like the code below 我找到了一个解决方案,已将<input>标记内容和标记<a>更改为<button> ,如下面的代码

<input type="text" class="form-control mr-sm-2" [(ngModel)]="query" name="busca">
<button type="submit" class="btn btn-light my-2 my-sm-0" (click)="search()">Buscar</button>

And in my component I've the code below 在我的组件中,我有下面的代码

export class MyComponent implements OnInit {
    query = ''; // Global variable in my component

    constructor() {}

    ngOnInit() {
    }

    search() {
        if (this.query !== '') {
            window.location.href = 'http://mydomain/result.html?name=' + this.query;
        }
    }
}

And on my app.module.ts I've added the following lines 在我的app.module.ts添加了以下几行

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // Imported the FormsModule here

import { AppComponent } from './app.component';
import { MyComponent } from './my/my.component';

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    MyComponent
  ],
  imports: [
    BrowserModule,
    FormsModule  // Added the FormsModule here
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

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

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