2015-12-13 27 views
1

我想實現在一個有角度的js網站谷歌自定義搜索。 當我點擊搜索按鈕時,它不會顯示任何東西,但網址更新爲網址。 我已經按照谷歌文檔中提到的步驟。 我不知道我在做什麼錯了?在angularjs中實現谷歌自定義搜索

我的搜索欄位於主頁上 -

<gcse:searchbox-only enableAutoComplete="true" resultsUrl="#/searchresult" lr="lang_en" queryParameterName="search"></gcse:searchbox-only> 

我的搜索結果有 -

<gcse:searchresults-only lr="lang_en"></gcse:searchresults-only> 

任何輸入是非常讚賞。

感謝,

回答

2

你可能有一個以上的問題,在同一時間發生的事情......

1.查詢參數不匹配

searchresults-onlygcse:searchbox-only指定queryParameterName不匹配。

的index.html

<gcse:searchresults-only queryParameterName="search"></gcse:searchresults-only> 

Search.html

<gcse:searchresults-only queryParameterName="search"></gcse:searchresults-only> 

2. Angular.js阻止谷歌CSE

的流動在正常情況下, Google搜索元素將觸發HTTP GET w ith搜索參數。但是,由於您正在處理單頁應用程序,因此您可能看不到查詢參數。如果當你的目標resultsUrl="#/searchresult"這種懷疑是真的,那麼你有兩個選擇:在resultsUrl =

  1. 強制HTTP GET 「的http:// YOURWEBSITE /信息搜索結果」。您可能必須匹配路線,或沿着這些線路來捕捉REST請求(Ember.js很容易實現,但我還沒有在Angular.js中完成)。
  2. 使用JQuery和Angular .js從Index.html上的用戶獲得輸入,並手動觸發search.html上的搜索。你會怎麼做?對於index.html,你會做類似於下面的事情,並且爲了實現類似於我在另一個post中回答的結果。

的Index.html

<div>GSC SEARCH BUTTON HOOK: <strong><div id="search_button_hook">NOT ACTIVATED.</div></strong></div> 
<div>GSC SEARCH TEXT: <strong><div id="search_text_hook"></div></strong></div> 
<gcse:search ></gcse:search> 

Index.js

//Hook a callback into the rendered Google Search. From my understanding, this is possible because the outermost rendered div has id of "___gcse_0". 
window.__gcse = { 
    callback: googleCSELoaded 
}; 
//When it renders, their initial customized function cseLoaded() is triggered which adds more hooks. I added comments to what each one does: 
function googleCSELoaded() { 
    $(".gsc-search-button").click(function() { 
    $("#search_button_hook").text('HOOK ACTIVATED'); 
    }); 
    $("#gsc-i-id1").keydown(function(e) { 
    if (e.which == 13) { 
    $("#enter_keyboard_hook").text('HOOK ACTIVATED'); 
    } 
    else{ 
    $("#search_text_hook").text($("#gsc-i-id1").val()); 
    } 
    }); 
} 

(function() { 
    var cx = '001386805071419863133:cb1vfab8b4y'; 
    var gcse = document.createElement('script'); 
    gcse.type = 'text/javascript'; 
    gcse.async = true; 
    gcse.src = 'https://cse.google.com/cse.js?cx=' + cx; 
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(gcse, s); 
})(); 

我有index.html的代碼的live version,但我不做出承諾說會因爲它是在我的NDSU FTP中託管的,所以永久生活。