如何在頁面加載時(shí)執(zhí)行AngularJS控制器功能?目前,我有一個(gè)Angular.js頁面,允許搜索和顯示結(jié)果。用戶單擊搜索結(jié)果,然后單擊“后退”按鈕。我希望搜索結(jié)果再次顯示,但我無法確定如何觸發(fā)搜索來執(zhí)行。細(xì)節(jié)如下:我的Angular.js頁面是一個(gè)搜索頁面,有一個(gè)搜索字段和一個(gè)搜索按鈕。用戶可以手動(dòng)輸入查詢并按下一個(gè)按鈕,Ajax查詢就會(huì)被觸發(fā)并顯示結(jié)果。我用搜索詞更新URL。一切都很好。用戶點(diǎn)擊搜索結(jié)果,并被帶到另一個(gè)頁面-這也很好。用戶單擊后退按鈕,然后返回到我的角度搜索頁面,并顯示正確的URL,包括搜索詞。一切正常。我已經(jīng)將搜索字段值綁定到URL中的搜索項(xiàng),因此它包含了預(yù)期的搜索項(xiàng)。一切正常。如何使搜索函數(shù)再次執(zhí)行,而不需要用戶按下“搜索按鈕”?如果是jQuery,那么我將在document戰(zhàn)備函數(shù)中執(zhí)行一個(gè)函數(shù)。我看不到Angular.js的等價(jià)物。
3 回答

至尊寶的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
// at the bottom of your controllervar init = function () { // check if there is query in url // and fire search in case its value is not empty};// and fire it after definitioninit();
// register controller in html<div data-ng-controller="myCtrl" data-ng-init="init()"></div>// in controller$scope.init = function () { // check if there is query in url // and fire search in case its value is not empty};
ng-init
ng-init
<div data-ng-controller="myCtrl" data-ng-init="init('%some_backend_value%')"></div>

慕容森
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
$scope.$on('$viewContentLoaded', function() { //call it here});

慕哥9229398
TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊
$viewContentLoaded
ng-init
ng-repeat
$scope.$on('$routeChangeSuccess', function () { // do something});
ui-router
$scope.$on('$stateChangeSuccess', function () { // do something});
- 3 回答
- 0 關(guān)注
- 1267 瀏覽
添加回答
舉報(bào)
0/150
提交
取消