Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute AngularJS controller function on page load?

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:

  • 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.

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.

like image 872
Duke Dougal Avatar asked Mar 17 '13 07:03

Duke Dougal


People also ask

How pass data from controller controller to AngularJS?

Approach: To share data between the controllers in AngularJS we have two main cases: Share data between parent and child: Here, the sharing of data can be done simply by using controller inheritance as the scope of a child controller inherits from the scope of the parent controller.

How can you initialize a select box with options on page load?

With options on page load how you can initialize a select box ? ng-init(in page load) & ng-selected(for select box) will be helpful.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.


2 Answers

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(); 

Also you can take a look at ng-init directive. Implementation will be much like:

// 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 }; 

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.

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> 
like image 183
Dmitry Evseev Avatar answered Sep 17 '22 17:09

Dmitry Evseev


Try this?

$scope.$on('$viewContentLoaded', function() {     //call it here }); 
like image 38
holographic-principle Avatar answered Sep 21 '22 17:09

holographic-principle