2014-06-11 3 views
7

मैं कोणीय जेएस के लिए अपेक्षाकृत नया हूं और मैं लंबे समय से jQuery का उपयोग कर रहा हूं। यही कारण है कि मुझे मेरे अंदर jQuery को कोणीय में स्थानांतरित करने में कठिनाई हो रही है। : डी

मैं जानना चाहता हूं कि हम कोणीय में एक डोम क्वेरी कैसे कर सकते हैं। मूल रूप से, मैं एक स्थिति मैं कहाँ है का सामना करना पड़ रहा इस

$(".myClass").each(function(){ 
    $(this).doSomething(); 
}) 

की तरह कुछ किसी को भी मुझे बता सकते हैं कैसे करना एक कोणीय प्रोग्रामर कुछ इस तरह करना होगा।

धन्यवाद

AngularJS में
+1

आप [? __How मैं "AngularJS में लगता है कि" कि मेरा कोई jQuery पृष्ठभूमि है __] के माध्यम से जाना चाहिए (http://stackoverflow.com/questions/14994391/how- do-i-think-in-angularjs-if-i-have-a-jquery-background) पहले। – Satpal

उत्तर

4

डोम जोड़तोड़ नियंत्रक, सेवाओं आदि में नहीं होना चाहिए ... लेकिन यह यह निर्देशों ...

केवल एक ही स्थान पर होना चाहिए, अगर आप एक हेरफेर करना चाहते डोम आप के निर्देश का उपयोग करें और वहाँ में अपने हेरफेर करना चाहिए ...

यहाँ AngularJS में डोम जोड़तोड़ के बारे में कुछ अच्छी लेख है ...

Best Practice - Dom Manipulations

DOM Manipulation in AngularJS — Without jQuery

अब के एक निर्देश बनाने की तरह आप चाहते हैं की कोशिश करते हैं। ऐसा लगता है कि आप उन्हें अपनी कक्षा के माध्यम से चुनकर तत्व में हेरफेर करना चाहते हैं। ठीक है कोई समस्या नहीं तो हम (सब कुछ दिखाने के लिए वर्बोज़ संस्करण) एक निर्देश जो restrict:'C' मतलब है कक्षा है ...

यहाँ

हमारे निर्देश घोषणा है ... बनाने के लिए

app.directive('myClass',function(){ 
    // Runs during compile 
    return { 
     // name: '', 
     // priority: 1, 
     // terminal: true, 
     // scope: {}, // {} = isolate, true = child, false/undefined = no change 
     // controller: function($scope, $element, $attrs, $transclude) {}, 
     // require: 'ngModel', // Array = multiple requires, ? = optional,^= check parent elements 
     restrict: 'C', // E = Element, A = Attribute, C = Class, M = Comment 
     // template: '', 
     // templateUrl: '', 
     // replace: true, 
     // transclude: true, 
     // compile: function(tElement, tAttrs, function transclude(function(scope, cloneLinkingFn){ return function linking(scope, elm, attrs){}})), 
     link: function($scope, iElm, iAttrs, controller) { 
      console.log('Here is your element', iElm); 
        // DO SOMETHING 
     } 
    }; 
}); 

यहाँ PLUNKER है की जरूरत है। .. $('selector') के लिए

2

वैकल्पिक angular.element('selector') और $.each के लिए वैकल्पिक angular.forEach है। इस प्रकार अपने कोड लगेगा जैसे:

var els = angular.element('.myClass'); 
angular.forEach(els, function(el){ 
    angular.element(el).doSomething(); 
}) 
+1

ध्यान दें कि 'angular.element' केवल चयनकर्ताओं का समर्थन करता है अगर आपने कोणीय से पहले jQuery लोड किया है। – tasseKATT