Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS declaring variable with initial value in View?

Tags:

angularjs

Is there a way to set the initial value of a variable in view? What I mean is:

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

So that myVar is now set to 2, and I can access it via $scope.myVar as well when I want and change its value.

like image 687
Kousha Avatar asked Mar 23 '14 04:03

Kousha


1 Answers

Use the ngInit directive:

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

Your value will be initialized, and accessible via $scope.myVar in your controller.

like image 132
DRiFTy Avatar answered Oct 14 '22 23:10

DRiFTy