目前我有一个Angular.js页面,允许搜索和显示结果。用户单击搜索结果,然后单击返回按钮。我想再次显示搜索结果,但我不知道如何触发搜索执行。细节如下:

My Angular.js page is a search page, with a search field and a search button. The user can manually type in a query and press a button and and ajax query is fired and the results are displayed. I update the URL with the search term. That all works fine. User clicks on a result of the search and is taken to a different page - that works fine too. User clicks back button, and goes back to my angular search page, and the correct URL is displayed, including the search term. All works fine. I have bound the search field value to the search term in the URL, so it contains the expected search term. All works fine.

我如何让搜索功能再次执行,而不需要用户按下“搜索按钮”?如果是jquery,那么我会在documentready函数中执行一个函数。我找不到Angular.js的等价物。


当前回答

你可以使用angular的$window对象:

$window.onload = function(e) {
  //your magic here
}

其他回答

试试这个?

$scope.$on('$viewContentLoaded', function() {
    //call it here
});
angular.element(document).ready(function () {

    // your code here

});

Dimitri /Mark的解决方案不适合我,但使用$timeout函数似乎工作得很好,以确保您的代码只在标记呈现后运行。

# Your controller, including $timeout

var $scope.init = function(){
 //your code
}

$timeout($scope.init)

希望能有所帮助。

你可以使用angular的$window对象:

$window.onload = function(e) {
  //your magic here
}

另一个选择:

var myInit = function () {
    //...
};
angular.element(document).ready(myInit);

(通过https://stackoverflow.com/a/30258904/148412)