9951 explained code solutions for 126 technologies

angularjsHow do I use an AngularJS variable in a template?

An AngularJS variable can be used in a template by assigning it to the $scope object in the controller. The variable can then be accessed from the template using the {{}} syntax.

For example, the following code will assign a variable called name to the $scope object in the controller and display it in the template:


$scope.name = 'John';


<p>Hello {{name}}!</p>

Output example

Hello John!

Code explanation

  • $scope.name = 'John'; - assigns the string John to the variable name on the $scope object in the controller
  • {{name}} - accesses the name variable from the template

Helpful links

Edit this code on GitHub