I’m still new to working with Ionic, so I’m sure this is something I’m doing wrong. Working with a Phonegap project using Ionic 1.0.0b3, I’m trying to figure out how to add tap events on a button. I’ve tried a few things but can’t get the tap event to trigger. I can’t find any clear documentation on setting up gestures so I feel a little blind working through this.
At the most basic, here’s an example of what I tried:
From what I’ve seen, I thought angular/Ionic automatically bound events on anything with ng-click, but nothing I try gets it working. I’ve also tried a few things to bind on the element directly but can’t seem to get it.
I’m working primarily from the browser at this point in order to get the console. Nothing is being thrown, which has me all the more puzzled. Putting the alert straight in the directive should work, but in a function call also fails.
I’m loading ionic.bundle.js - I assume that has everything I need, Ionic/Angular/Hammer and I don’t need to load something else to get this to work?
Also, a little more context on what I’m doing: I have an app with a nav bar, tab bar, and side drawer. I’ve got a button on the nav bar that should toggle the drawer. I’ve tested my drawer method from the console and it works fine, I just can’t get it to fire from the button in the nav bar. I can’t get anything - even a basic alert - to fire from the button.
Essentially, yes. I’m calling a function inside a globally-scoped object. It fires when I do onClick but not ng-click. If it wasn’t finding the function, I’d expect to see an error in the console, but nothing at all shows up in console.
Evidently that was the problem! I put the method under rootScope and it works. Thanks for the solution. I’m surprised it hadn’t been throwing an error to console.
This raises an additional question. The button is in my nav bar. What scope does it look in when it gets tapped? If I wanted to set something specifically on its scope, where would it go since there is no nav bar controller?
The location where the root scope is attached to the DOM is defined by the location of ng-app directive. Typically ng-app is placed on the element, but it can be placed on other elements as well, if, for example, only a portion of the view needs to be controlled by Angular.
UI functions, including nav bar, should be in a controller. You can place a controller directive on the <body> element, at or below the element with the ng-app directive.