Change class with ng-click in AngularJS -


I have two buttons that change the square of an element. Each button represents a different class.

I try to pass the $ index in ng-click but it seems that it is not working properly .. any suggestions would be greatly appreciated! : -)

Some Styling

   

View

  & lt; Div ng-model = "myClasses" & gt; & Lt; A href = "#" ng-click = "item click ($ index)" & gt; 1'Arean & lt; / A & gt; & Lt; A href = "#" ng-click = "item click ($ index)" & gt; 2'Arn & lt; / A & gt; & Lt; P class = "{{myClasses}}" Torben {{myClasses}} & lt; / P & gt; & Lt; / Div & gt;  

and my angular

  & lt; Script & gt; Var app = angular Module ('plunker', []); App.controller ('MainCtrl', function ($ scope) {$ scope.myClasses = "class0"; $ scope.itemClick = function ($ index) {if ($ index == 1) {$ scope.myClasses = "class1 ";} And if ($ index == 2) {$ scope.myClasses =" class2 ";} and {$ scope.myClasses =" class1 ";}};}); & Lt; / Script & gt;  

$ index is not defined in view (it Mainly defined within the ng-repeat instructions) you can do this just replace the html code:

   Torben {{myClasses}} & lt; / P & gt; & Lt; / Div & gt;  

Comments