AngularJS declares a variable with an initial value in a view?

Is there a way to set the initial value of a variable? I mean:

<div> {{ myVar = 2 }} </div> 

So, myVar now set to 2, and I can access it through $scope.myVar when I want and change its value.

+6
source share
2 answers

Use the ngInit directive:

 <div ng-init="myVar = 2"> {{ myVar }} </div> 

Your value will be initialized and available through $scope.myVar in your controller.

+16
source

To combine two variable values ​​using delimiter / underscore (_) in HTML using AngularJS

 <div ng-init="item_id = (variable_1 + '_' + variable_1)"></div> 
0
source

All Articles