7

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.

2 Answers 2

17

Use the ngInit directive:

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.