Kousha
Kousha

Reputation: 36219

AngularJS declaring variable with initial value in View?

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.

Upvotes: 7

Views: 20488

Answers (2)

rc.adhikari
rc.adhikari

Reputation: 2222

To Concatenate the two variable value using delimiter/underscore(_) in HTML using AngularJS

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

Upvotes: 0

DRiFTy
DRiFTy

Reputation: 11369

Use the ngInit directive:

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

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

Upvotes: 17

Related Questions