What does :: mean in angularJS

Angularjs

Angularjs Problem Overview


I have seen a syntax like

<a href={{ ::something}}>some other thing</a>

What is that double colon for? What happens if it is removed?

Angularjs Solutions


Solution 1 - Angularjs

:: is used for one-time binding. The expression will stop recalculating once they are stable, i.e. after the first digest.

So any updates made to something will not be visible.

Solution 2 - Angularjs

It's used to bind model from your controller to view only. It will not update your controller model if you change this from your view. It means it's used to achieve one time binding.

Example

angular.module("myApp", []).controller('ctrl', ['$scope', function($scope) {
$scope.label = 'Some text';
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<html ng-app="myApp">
  <body ng-controller="ctrl">  
    <div>{{::label}}</div> // this will print `Some text` on load
    <div>{{label}}</div> // this will too print `Some text` on load
    <br />
    <button ng-click="label='IUpdateLabelFromHtml'">Change label</button>
  </body>
 </html>

When we change label meaning when we click on Change label link, it will update only second text i.e. binded without :: operator.

Read this for more details One way binding

Solution 3 - Angularjs

It means that the scope item "something" has one time binding associated to it. Thus should the item change in the controller the change will not be applied.

This is a good article on watchers and one time bindings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaryamView Question on Stackoverflow
Solution 1 - AngularjsVivekView Answer on Stackoverflow
Solution 2 - AngularjsJay ShuklaView Answer on Stackoverflow
Solution 3 - AngularjsMike SavView Answer on Stackoverflow