0

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>
5
  • 1
    it must be ng-click="eventDescription()", missing parenthesis Commented May 25, 2016 at 18:44
  • Actually I have it like that, I just update the question, in my console I got the correcto message but the issue is that I cant switch to the view in the "summary" button. Commented May 25, 2016 at 18:48
  • Are the buttons in the same scope as the ng-shows? Commented May 25, 2016 at 18:50
  • 1
    Your "Summary" Button has two 'ng-click' attributes. that's not going to work. Commented May 25, 2016 at 18:52
  • @ajmajmajma yes, the are, actually I already notice that I have a previos ng-click, ng-click="showData = 2" for chosing the directives so I add a second ng-click but I also think this is wrong. Commented May 25, 2016 at 18:53

3 Answers 3

1

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time, I just notice the two ng-click but I didnt know how to call to different functions!! This works perfect. =)
1

On ng-click on both buttons, it must call the function eventDescription.

In your original code ... The first button was just changing the value of $scope.showData. The second button had ng-click twice, once doing 'eventDescription' instead of 'eventDescription()', and the other was changing the value of $scope.showData.

If on the button click you want the value of $scope.showData to be changed and to call the function $scope.eventDescription, you should just changet it in the function... You can, however, do this on ng-click as well

ng-click="showData = 1 && eventDescription()"

I'd prefer doing it in the function as it would take up less space in the html.

<div class="btn-group">
 <button ng-click="eventDescription(1)">Headline</button>
 <button ng-click="eventDescription(2)" >Summary</button>
</div>

$scope.eventDescription = function (viewNumber) {
    $scope.showData = viewNumber;
    console.log("Button Clicked");
}

1 Comment

Thanks for your time, I really appreciate it =)
0

Some points you can follow:

  1. You should use ng-model for input fields not in others tag so should not use in button tag
  2. Better to initialize in controller $scope.showData = 1;
  3. btn class is common so use in class="btn" no need in condition

so you can try it like:

HTML:

<div class="btn-group">
     <button type="button" class="btn" ng-class="showData===1? 'btn-primary' : 'btn-white' " ng-click="showData = 1">Headline</button>
     <button type="button" class="btn" ng-class="showData===2? 'btn-primary' : 'btn-white' " ng-click="eventDescription()" >Summary</button>
 </div>

Controller:

$scope.showData = 1; // initialize

$scope.eventDescription = function () {
   $scope.showData = 2; // set 2 for showData
   //some code goes here
    console.log("Service OK")
};

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.