kennechu kennechu - 6 months ago 18
AngularJS Question

Calling a function from controller to my view using ng-click

I have this two buttons.

<div class="btn-group" ng-init="showData = 1">
<button ng-model="showData" type="button" ng-class='{"btn btn-primary": showData == 1, "btn btn-white": showData != 1}' ng-click="showData = 1">Headline</button>
<button ng-click="eventDescription()" ng-model="showData" type="button" ng-class='{"btn btn-primary":showData == 2, "btn btn-white": showData != 2}' ng-click="showData = 2">Summary</button>
</div>


In my controller I have a function that calls a WS, in the console I print a message just to make sure it's working and I'm able to see it,
my function is something like this:

$scope.eventDescription = function () {

some code goes here

console.log("Service OK")

}


The function code is not relevant so that's why I didnt put the code

The thing is that when I click on the button to swith to that view nothing happens, I can't switch to that view so I dont get what is wrong if Im using ng-click.

Maybe I'm missing something but Im new to angular.

also I think this is relevant, depending on the button selection I call different directives (I render some HTML on them) like this:

<head-line-view ng-show="showData == 1"></head-line-view>
<summary-view ng-show="showData == 2"></summary-view>

Answer

you have 2 ng-click attributes in button tag, try this

 <button ng-click="eventDescription();showData = 2" ng-model="showData" type="button" ng-class='{"btn btn-primary":showData == 2, "btn btn-white": showData != 2}'>Summary</button>