Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ng-init and ng-bind?

Tags:

html

angularjs

Below is the code using ng-init:

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

<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
  <p>Looping with ng-repeat:</p>
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

</body>
</html>

Below is the code using ng-bind:

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

<div ng-app="" ng-bind="names=['Jani','Hege','Kai']">
  <p>Looping with ng-repeat:</p>
  <ul>
    <li ng-repeat="x in names">
      {{ x }}
    </li>
  </ul>
</div>

</body>
</html>

Both give the same output, with different formats.

What exactly is happening here ?

Please help!

like image 391
Pritish Avatar asked Mar 04 '26 20:03

Pritish


1 Answers

The purpose of ng-init is initialize a property in template.

The purpose of ng-bind is replace the text content of the specified HTML element with the value of a given expression.

But you should initialize the variable in controller and use {{variable}} and don´t use ng-init or ng-bind for this.

like image 93
zamarrowski Avatar answered Mar 07 '26 09:03

zamarrowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!