在页面加载执行的功能? [英] execute function on page load?
问题描述
目前我有一个Angular.js页面,让搜索和显示结果。在搜索结果用户点击,然后单击后退按钮。我想要的搜索结果再次显示,但我不知道如何来触发执行搜索。这里的细节:
Currently I have an Angular.js page that allows searching and displays results. User clicks on a search result, then clicks back button. I want the search results to be displayed again but I can't work out how to trigger the search to execute. Here's the detail:
- 我Angular.js页面是一个搜索页面,与搜索领域,搜索
按钮。用户可以手动输入一个查询和preSS一个按钮,
和Ajax查询被触发,并显示结果。我更新了搜索项的URL。这一切工作正常。 - 用户点击搜索的结果,并采取不同的页面 - 太正常工作。
- 用户点击后退按钮,并回到我的角度搜索页面,并显示正确的URL,其中包括搜索词。一切工作正常。
- 我已绑定搜索字段值在URL中搜索词,所以它包含了预期的搜索词。一切工作正常。
我如何获得的搜索功能,而无需preSS搜索键,用户再次执行?如果是jQuery的话,我将在documentready函数执行的功能。我看不到Angular.js等价的。
How do I get the search function to execute again without the user having to press the "search button"? If it was jquery then I would execute a function in the documentready function. I can't see the Angular.js equivalent.
推荐答案
在一方面为@马克 - Rajcok说你可以只带私人内部函数脱身:
On the one hand as @Mark-Rajcok said you can just get away with private inner function:
// at the bottom of your controller
var init = function () {
// check if there is query in url
// and fire search in case its value is not empty
};
// and fire it after definition
init();
您也可以看看 NG-初始化指令。实施将是很像:
// 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
};
不过,要关心它作为角文档暗示(自V1.2)不使用 NG-INIT
为。然而伊莫这取决于你的应用程序的架构。
But take care about it as angular documentation implies (since v1.2) to NOT use ng-init
for that. However imo it depends on architecture of your app.
我用 NG-INIT
时,我想通过从后端的值转变为角应用:
I used ng-init
when I wanted to pass a value from back-end into angular app:
<div data-ng-controller="myCtrl" data-ng-init="init('%some_backend_value%')"></div>
这篇关于在页面加载执行的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!