2015-06-09 12 views
5

लागू करने के निर्देश में मुझे निम्नलिखित निर्देश में $ संकलन इंजेक्शन देने में समस्याएं हैं।

export class Element { 
    public link(scope:dirScopeInterface, element:any, attrs:ng.IAttributes, formCtrl:ng.IFormController) { 
     var attr = this.arrayJoiner(scope.standard, scope.attrs || {}, scope.ignore || {}); 
     element.html(this.compiler(attr)); 
     $compile(element.contents())(scope); 
    } 
} 

फिलहाल यह एक $ संकलन फेंक रहा है अपरिभाषित त्रुटि है। मैंने

static $inject = ['$compile']; 

का उपयोग करने का प्रयास किया है लेकिन यह कुछ कारणों से पारदर्शी लिपि से गायब हो जाता है।

Here पूर्ण कोड का उपयोग किया गया है।

उत्तर

0

तो मुझे इसे काम करने के लिए एक रास्ता मिला लेकिन यह उतना ही सुरुचिपूर्ण नहीं है जितना मुझे पसंद आया।

angular.module('formDirectives', [], function($compileProvider){ 
    $compileProvider.directive('catElement', ($compile) => { 
     return new Element($compile); 
    }); 
}) 
9

static $inject और एक constructor को शामिल करें:

export class Element { 

    // $compile can then be used as this.$compile 
    constructor(private $compile: ng.ICompileService){}; 

    public link(scope:dirScopeInterface, element:any, attrs:ng.IAttributes, formCtrl:ng.IFormController) { 
     var attr = this.arrayJoiner(scope.standard, scope.attrs || {}, scope.ignore || {}); 
     element.html(this.compiler(attr)); 
     this.$compile(element.contents())(scope); 
    } 
} 

संपादित

कोणीय साथ इस निर्देश को पंजीकृत करने के, यह क्या मैं हमेशा है (वहाँ कई समाधान कर रहे हैं):

export class Element implements angular.IDirective { 

    public static ID = "element"; 

    // This can then be removed: 
    // static $inject = ["$compile"]; 

    // .. 

    /** 
    * The factory function that creates the directive 
    */ 
    static factory(): angular.IDirectiveFactory { 
     const directive = ($compile) => new Element($compile); 
     directive.$inject = ["$compile"]; 
     return directive; 
    } 
} 

और पंजीकरण करने के लिए:

angular.module("myModule" []) 
    .directive(Element.ID, Element.factory()); 
+0

मैं इस निर्देश को कोणीय के साथ कैसे पंजीकृत करूं, क्योंकि कन्स्ट्रक्टर को पैरामीटर की आवश्यकता होती है? इसके अलावा मैंने इसे आपके द्वारा निर्दिष्ट तरीके से कार्यान्वित किया है लेकिन एक $ संकलन प्राप्त करना एक फ़ंक्शन त्रुटि नहीं है। – Rockroxx

+0

कोणीय निर्भरता इंजेक्शन का उपयोग करता है, इसलिए कन्स्ट्रक्टर स्वयं को हल करेगा। निजी $ संकलन चर प्राप्त करने के लिए आपको नियंत्रक को सूचक का उपयोग करना होगा: 'यह। $ संकलन' – devqon

+0

ऐसा लगता है कि: स्थिर $ इंजेक्ट = ['$ संकलन']; कन्स्ट्रक्टर (निजी $ संकलन: ng.ICompileService) {} सार्वजनिक लिंक = (दायरा: dirScopeInterface, तत्व: कोई, attrs: ng.IAttributes, formCtrl: ng.IFormController) => { scope.tempForm = formCtrl; var attr = this.arrayJoiner (scope.standard, scope.attrs || {}, scope.ignore || {}); element.html (this.compiler (attr)); यह। $ संकलन (element.contents()) (दायरा); } कोणीय एक $ संकलन फेंकता त्रुटि समारोह नहीं फेंकता है। – Rockroxx

संबंधित मुद्दे