Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store variable in an AngularJS HTML template?

Tags:

angularjs

How can I store values to variable for example on click in angular? I have the following code which doesn't save the value into variable:

HTML:

<div ng-app='app'>
    <a ng-controller="MyController" href="#" ng-click="variable = 1">

    </a>
    {{variable}}
</div>

Controller:

var app = angular.module('app',[]);

app.controller('MyController', function($scope)
{
});

jsfiddle code

like image 611
Runtime Terror Avatar asked Mar 26 '26 19:03

Runtime Terror


1 Answers

Your variable binding {{variable}} should be inside controller's scope. So move ng-controller to an element that will contain variable binding.

like image 84
kasimoglou Avatar answered Mar 29 '26 08:03

kasimoglou