1

I try this method , but it is not working for me , I am beginner in AngulartJS

var getDb = angular.module('dataapp', [])
  .controller('datagetcontroller', function ($scope,$window) {
    $window.onload = function(){
      alert('this is test')
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<tr ng-app="dataapp" ng-controller="datagetcontroller"><td>loremipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td></tr>

Thank in advance.

--UPDATE-- Maybe I have problem , because before new page load I use page redirect via

window.location.href

, which help I redirect to page where I need to execute javascript.

  • I have the same problem , that is why I ask experts to help me. I need for example when page load finish . Alert me something –  Aug 04 '17 at 08:26
  • Take a look at [this post](https://stackoverflow.com/q/43877226/6712896) or [this one](https://stackoverflow.com/q/13059284/6712896) or [this one](https://stackoverflow.com/questions/15458609/how-to-execute-angular-controller-function-on-page-load) – JeanJacques Aug 04 '17 at 08:32

3 Answers3

2
    var getDb = angular.module('dataapp', [])
    .controller('datagetcontroller', function ($scope) {
    //assign to scope function
    $scope.load = function() {
           alert("Window is loaded");
     }}});



 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
//call load function in ng-init
<tr ng-app="dataapp" ng-controller="datagetcontroller" ng-init="load()"><td>loremipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td></tr>
Arun kumar
  • 1,535
  • 3
  • 12
  • 17
  • Sorry , but it did not help , please if you can look I have just updated my answer. –  Aug 04 '17 at 09:19
1

I have also added a working plunkr https://plnkr.co/edit/DD0BeZLqZbZcoxCDNi1g?p=preview. Suppose your .js file is script.js which looks like:

var getDb = angular.module('dataapp', [])
.controller('datagetcontroller', function ($scope,$window) { $window.onload = function(){ alert('this is test') } });

and your html page is index.html where head should look like have: <head> <link rel="stylesheet" href="style.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script> <script src="script.js"></script> </head> <body ng-app="dataapp"> <table> <tr ng-controller="datagetcontroller"> <td>loremipsum</td> <td>Lorem ipsum</td> </tr> </table> </body>

It should work fine. I think you have not declared the script.js file in the index.html where you have written the javascript. Also note that, you have to place the angular.min.js script above the script.js as angular needs to load before hand so that script.js understands the code.

JEET ADHIKARI
  • 529
  • 2
  • 6
  • 19
  • Your noted solution has not helped . But Thank. –  Aug 04 '17 at 09:13
  • are you facing any error in the console? did you check the plunkr link I have attached? – JEET ADHIKARI Aug 04 '17 at 09:34
  • Yes I have check your attached solution , but it does not execute javascript in my situation. But in plunker it works perfect. I have just update my answer , maybe my problem is my updated answer . –  Aug 04 '17 at 09:38
0

This was my solution

angular.element(window).bind('load', function() {
    
});
Other reason that my angular did not work , was in a simple mistake , I do not have right to use more than one ng-app="" inside one html file