I want to compare ng-init vs ng-onload and is there any other similar way to call an on load function?

562 Views Asked by At

I am developing a dashboard with angular js (Angular 1.5). In my parent interface(Interface A), on a button click it needed to be popped an update user information form(Interface B). Popping up a form works fine.

But my problem is, I need to make it fill the details of that update form(Interface B) if the data is already there in the DB. DB query which is in the controller of Interface B; needs a parameter in interface B to retrieve data(That parameter changes time to time).

So my problem is, if I use "ng-init=myfunction()" in the code of Interface B, it gives undefined because the needed parameter is only instantiated on the button click of Interface A ; to pop the update form (Interface B). If I use ng-onload instead, it doesn't give any relevant output.

As I did not have a proper mechanism for this, what I did was to add another button on my interface B, So that on the button click(ng-click) event I could call my function to retrieve data to my form.

Please help me with this issue.

Thank you in advance.

1

There are 1 best solutions below

1
Rahul Sharma On

Try this

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  {{initData}}
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
 $scope.initData = 'Before';
    function onInit(){
     $scope.initData = 'After Init';
     console.log('OnInit');
    }
    onInit();
});
</script>

</body>
</html>