2014-10-02 8 views
15

मेरे पास एक तत्व है जो ng-mousedown और ng-mouseup पर फ़ंक्शन को सक्रिय करता है। हालांकि, यह टच स्क्रीन पर काम नहीं करता है, क्या ng-touchstart और ng-touchend जैसे कोई निर्देश है?एनजी-टचस्टार्ट और एनजी-टचचेन्ड Angularjs

उत्तर

13

इस के लिए एक मॉड्यूल है: https://docs.angularjs.org/api/ngTouch

लेकिन तुम भी घटनाओं के लिए अपने स्वयं के निर्देशों लिख सकते हैं:

<!doctype html> 
 
<html> 
 
    <head> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script> 
 
    </head> 
 
    <body ng-app="plunker"> 
 
     <div ng-controller="MainCtrl"> 
 
      <div my-touchstart="touchStart()" my-touchend="touchEnd()"> 
 
       <span data-ng-hide="touched">Touch Me ;)</span> 
 
       <span data-ng-show="touched">M-m-m</span> 
 
      </div> 
 
     </div> 
 
     <script> 
 
      var app = angular.module('plunker', []); 
 
      app.controller('MainCtrl', ['$scope', function($scope) { 
 
       $scope.touched = false; 
 

 
       $scope.touchStart = function() { 
 
        $scope.touched = true; 
 
       } 
 

 
       $scope.touchEnd = function() { 
 
        $scope.touched = false; 
 
       } 
 
      }]).directive('myTouchstart', [function() { 
 
       return function(scope, element, attr) { 
 

 
        element.on('touchstart', function(event) { 
 
         scope.$apply(function() { 
 
          scope.$eval(attr.myTouchstart); 
 
         }); 
 
        }); 
 
       }; 
 
      }]).directive('myTouchend', [function() { 
 
       return function(scope, element, attr) { 
 

 
        element.on('touchend', function(event) { 
 
         scope.$apply(function() { 
 
          scope.$eval(attr.myTouchend); 
 
         }); 
 
        }); 
 
       }; 
 
      }]); 
 
     </script> 
 
    </body> 
 
</html>

+0

मैंने कल इस मॉड्यूल को स्थापित किया और मैंने इस पोस्ट को बाद में लिखा। मैं इन निर्देशों के साथ प्रयास करूंगा। – ciembor

+2

कोणीय की एनजी टच लाइब्रेरी व्यक्तिगत स्पर्श और रिलीज घटनाओं के लिए बाध्यकारी का समर्थन नहीं करती है: http://stackoverflow.com/questions/19985097/can-angulars-ngtouch-library-be-used-to-detect-a-long-click- टच -होल्ड-रिलीज – Egg

7

मैं उन ealier आज बना दिया है के बाद से मैं यह आवश्यक स्वयं:

आशा है कि यह मदद करता है।

+0

मैं ngTouchstart का उपयोग करना चाहता हूं, लेकिन दुर्भाग्य से यह eval() का उपयोग करता है, जो क्रोम ऐप में टूट जाएगा। –

+0

इसके बजाय इसे '$ scope। $ Apply' का उपयोग करने के लिए अपडेट किया गया। –

+1

धन्यवाद! क्या यह निर्देश से बाएं या दाएं स्थानांतरित करने का फैसला कर सकता है? – Martian2049

1

संस्करण जो हम $ parse() का उपयोग करते हैं, जो $ eval() आंतरिक रूप से उपयोग करता है। हम विशेष रूप से एक निर्देश का उपयोग करके mousedown और टचस्टार्ट घटनाओं को संभालना चाहते थे, लेकिन कोणीय तरीके से हम कोणीय शैली अभिव्यक्तियों को शामिल कर सकते हैं।

तो जैसा

:

angular.module("ngStudentselect", []).directive('ngStudentselect', ['$parse', '$timeout', '$rootElement', 
function($parse, $timeout, $rootElement) { 
    return function(scope, element, attr) { 
     var clickHandler = $parse(attr.ngStudentselect); 

     element.on('mousedown touchstart', function(event) { 
      scope.$apply(function() { 
       clickHandler(scope, {$event: event}); 
      }); 
     }); 
    }; 
}]); 

कोणीय के ngClick निर्देश का यह एक नीचे छीन संस्करण है।