Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: accessing the ngTouch service from a directive?

Tags:

angularjs

I really love how the new ng-click directive in Angular now automatically includes functionality for touch events. However, I am wondering if it is possible to access that touch-event service from my custom directive? I have lots of directives that require that I bind a click event to the given element, but I'm simply doing that using the typical jquery syntax (ex: element.on('click', function(){ ... })). Is there a way that I can bind an ng-click event to an element within a directive? Without having to manually put a ng-click tag on my element in the HTML of my view...?

I want to be able to harness the power of both click and touch events. I could obviously import a library (such as HammerJS or QuoJS) but I would prefer not to have to do that, especially since Angular is already doing it.

I can access the $swipe service and bind different elements to that, but is there a similar service for ngTouch?

For reference, this is an example of when I would want to do this:

mod.directive('datepicker', ['$timeout', function($timeout){
     return {
         link: function(scope, elem, attrs){
             var picker = new DatePicker();

             elem.on('click', function(e){
                 picker.show();
             });

             // I would rather do something like:
             // elem.on('ngTouch', function(){ ... });
             //
             // or even:
             // $ngTouch.bind(elem, {'click': ..., 'touch': ...});
         }
     }
}]);

UPDATE: As noted by below, the source code for the ng-click directive is here. Can anyone see a way to harness that code and turn it into a "bindable" service?

like image 991
tennisgent Avatar asked Oct 09 '13 16:10

tennisgent


People also ask

Which directive initializes an AngularJS application?

The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

Which directive do we use to inform AngularJS about the parts controlled by it?

The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.

Which directive is used for data binding in AngularJS?

A - ng-bind directive binds the AngularJS Application data to HTML tags.

Which directive tells AngularJS what to do when an HTML element is clicked?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.


1 Answers

I don't think that's quite the right approach. I'd approach this by using a template within your directive and then using ngTouch within that.

mod.directive('datepicker', ['$timeout', function ($timeout) {
     return {
         template: '<div ng-touch="doSomethingUseful()"></div>',

         link: function (scope, elem, attrs) {
             var picker = new DatePicker();

             scope.doSomethingUseful = function () {
                 // Your code.
             }
         }
     }
}]);

UPDATE

Full example with additional attributes on the directive element:

http://codepen.io/ed_conolly/pen/qJDcr

like image 102
eddiec Avatar answered Oct 21 '22 14:10

eddiec