3

I have an HTML with following buttons:

<button ng-click='showStats(player.data,0)'>Death Match</button> 
<button ng-click='showStats(player.data,1)'>Champions Rumble</button> 
<button ng-click='showStats(player.data,2)'>Full Profile</button>

As you might have guessed, when any of the buttons are clicked, then corresponding player stats are shown. This works fine without issues. Now, the system has an additional feature in that a player can set which part of his profile to show as default.

So when the user visits a player profile, suppose the player has set his profile show preference to 1 (Champions Rumble). In this case I want the button click on the middle button to be simulated (only when page is loaded first time) so that the corresponding profile part is shown. Then the visitor can click any of the buttons and that part of the profile will get shown,as it is being done now.

Can this be done? I have been trying for hours now.Any help is greatly appreciated!

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69

3 Answers3

2
$scope.showStats(data, defaultOption); // in your controller when user profile loaded
Ruslanas Balčiūnas
  • 7,310
  • 3
  • 24
  • 41
0

If I understand correctly, when the button is clicked, you want to show something, but what that thing is depends on the state of your model. So I would run a function on ngClick, and have that function do a different thing depending on the state of the model.

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
-3

You can simulate click event by using jQuery.

$('#button2').click()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button onclick=$('div').html('button1_click')>Button 1</button>
<button onclick=$('div').html('button2_click') id=button2>Button 2</button>
<button onclick=$('div').html('button3_click')>Button 3</button>
<div></div>

In you code you can do something like that:

$('button:contains("Champions Rumble")').click()
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • Why would you simulate a click when you can invoke the function directly? – tao Nov 26 '15 at 23:34
  • 1
    Becuase the question was how to simulate a click – Aminadav Glickshtein Nov 26 '15 at 23:37
  • The question was how to simulate a click, the answer, as stated in the other two responses, is that you don't need to simulate a click. Your answer isn't an angular solution and yet you're arguing over the correct usage of ng-init. For the record, I don't believe there's anything wrong with calling a function on a controller on ng-init. It's arguably a better solution than calling the same function directly in the controller because you may have another view associated with the same controller which doesn't need to launch the stats on initialisation. – net.uk.sweet Nov 27 '15 at 00:47