Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between onLoad and ng-init in angular

Tags:

angularjs

I am learning angular. I don't understand what is difference between onLoad and ng-init for initialization of a variable. In which scope it creates this variable.

For example

<ng-include onLoad="selectedReq=reqSelected" src="'partials/abc.html'"></ng-include> 

OR

<ng-include ng-init="selectedReq=reqSelected" src="partials/abc.html"></ng-include> 

Please also give me some idea about isolated scope.

like image 447
Jay Shukla Avatar asked Aug 26 '13 10:08

Jay Shukla


People also ask

What is ng init in angular?

The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application.

Which of the below is required with Ng-include directive?

The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename. By default, the included file must be located on the same domain as the document.


1 Answers

ng-init is a directive that can be placed inside div's, span's, whatever, whereas onload is an attribute specific to the ng-include directive that functions as an ng-init. To see what I mean try something like:

<span onload="a = 1">{{ a }}</span> <span ng-init="b = 2">{{ b }}</span> 

You'll see that only the second one shows up.

An isolated scope is a scope which does not prototypically inherit from its parent scope. In laymen's terms if you have a widget that doesn't need to read and write to the parent scope arbitrarily then you use an isolate scope on the widget so that the widget and widget container can freely use their scopes without overriding each other's properties.

like image 95
Jesus is Lord Avatar answered Sep 30 '22 18:09

Jesus is Lord