2017-04-20 47 views
0

不觸發我想實現一個簡單的搜索框在我的應用程序搜索谷歌(因爲如果你在google.com) 不過,好像沒有什麼改變,一旦我點擊提交按鈕。 Angular2是否阻止表單觸發?表單搜索谷歌在Angular2

<form method="get" action="https://www.google.com/search"> 
    <input type="text" name="q" size="31" value=""> 
    <button type="submit" name="button">go</button> 
    </form> 
+0

如果你在瀏覽器中使用的開發工具,還有在控制檯中的任何消息? – DeborahK

+0

@DeborahK感謝您的回覆。不,控制檯中沒有錯誤。 –

+0

我只是將你的代碼粘貼到我有的應用程序中,並且它按預期工作。它提出了谷歌搜索我輸入的文本。 – DeborahK

回答

0

您可能已將FormsModule導入到您的模塊中。

請使用ngNoForm在您的窗體上:<form ngNoForm>或刪除FormsModule如果你不使用它。

默認情況下,除了那些標有ngNoForm屬性的窗體外,所有窗體上的角度窗體均被激活。在這裏閱讀更多:https://angular.io/docs/ts/latest/guide/forms.html

ngNoForm從哪裏來?

看看NgForm的文檔:https://v2.angular.io/docs/ts/latest/api/forms/index/NgForm-directive.html。選擇部分:form:not([ngNoForm]):not([formGroup])通知not([ngNoForm])

+0

我沒有將FormsModule導入組件。 –

+0

不是你的組件,你**「模塊」 **聲明該組件 –

+0

謝謝艾哈邁德!這工作! –

0

@DeboraK

這是我的解決方法: 我只是希望它是一種更簡單的方法。一旦用戶按下提交,我就會調用這個函數。

googleSearch() { 
    let str = this.googlestring.searchvalue; 
    let queryStr = str.split(' ').join('+'); 

    let googleStr = 'https://www.google.com/webhp#q='; 

    let completeQueryStr = googleStr.concat(queryStr); 

    console.log(this.googlestring.searchvalue); 
    window.location.href = completeQueryStr; 
    }