2015-11-21 12 views
16

मैं कोणीय 1.5.0-beta.2 के साथ काम करने के लिए एक एप्लिकेशन पाने के लिएकोणीय 1.5 घटक विधि templateUrl + समारोह

एक 'निर्देश' मैं निम्नलिखित कोड है बनाने के लिए कोशिश कर रहा हूँ:

myApp.component('gridshow', { 
    bindings: { 
    slides: '=' 
    }, 
    controller: function() { 

    }, 
    controllerAs: 'grid', 
    template: function ($element, $attrs) { 
    // access to $element and $attrs 
    return [ 
     '<div class="slidegrid">', 
     '<div ng-repeat="slide in grid.slides">', 
     '{{slide.image}}', 
     '</div>', 
     '</div>' 
    ].join('') 
    } 
}); 

मुझे टेम्पलेट का विचार पसंद है जो $element और $attrs तक पहुंच के साथ फ़ंक्शन देता है लेकिन मैं इसे टेम्पलेट यूआरएल के साथ कैसे जोड़ूं?

+3

आप उन्हें गठबंधन नहीं है, आप या तो एक या एक और का उपयोग करें। – dfsq

+0

मैं आश्चर्यचकित नहीं हूं, लेकिन थोड़ा सा dissapointed हूं। धन्यवाद dfsq। – tuvokki

+1

क्यों निराश। आप टेम्पलेट और टेम्पलेट यूआरएल दोनों के साथ क्या करना चाहते हैं? यह एक लिफ्ट के अंदर एक सीढ़ी के लिए पूछना पसंद है ... वे अनन्य हैं ... –

उत्तर

19

1.5.0-बीटा 2 templateUrl इंजेक्टर द्वारा बुलाया जाने वाला एक कार्य हो सकता है। $element और $attrsare injectedboth template and templateUrl functions में component, साथ ही साथ किसी भी अन्य निर्भरताओं में।

इसका मतलब है कि

... 
    templateUrl: function ($element, $attrs) { 
    // access to $element and $attrs 
    ... 
    return $attrs.uninterpolatedTemplateUrl; 
    } 

बजाय किया जा सकता है।

+1

और यह बाहरी टेम्पलेट को कैसे सफ़ेद करता है? टेम्पलेट यूआरएल सामान्य रूप से टेम्पलेट का संदर्भ रखता है। – tuvokki

+0

यह सही है। तो निर्णय टेम्पलेट यूआरएल पर आधारित किया जा सकता है उदा। तत्व गुण। इसके लिए आप इसका उपयोग करने की क्या योजना बना रहे थे? यदि आपको टेम्पलेट यूआरएल फ़ंक्शन में टेम्पलेट प्रतिक्रिया प्राप्त करने की उम्मीद है, तो नहीं, आपको अभी भी निर्देश में एक टेम्पलेट को संसाधित करना होगा (हमारे पास घटक में कोई लिंक फ़ंक्शन नहीं है, लेकिन नियंत्रक है)। – estus

+0

मैं समझता हूं, अनुमान है, और मेरे यहां कोई वास्तविक उद्देश्य नहीं था। बस सोच रहा। – tuvokki

3

@estus समाधान मेरे लिए तब तक काम करता था जब तक कि मैंने अपनी स्क्रिप्ट को गुमराह नहीं किया। Uglified यह निम्न त्रुटि दिया:

Error: [$injector:unpr] Unknown provider: eProvider <- e 

समाधान है कि मेरे लिए काम किया है:

['$element', '$attrs', function($element, $attrs) { 
    return $attrs.uninterpolatedTemplateUrl; 
}] 
+3

** एंगुलर में सभी ** फ़ंक्शंस जो DI का उपयोग करते हैं उन्हें उचित रूप से कम करने के लिए एनोटेट किया जाना चाहिए। अर्थात। निर्देशक कार्यों के अपवाद के साथ, कोणीय एपीआई में लगभग हर समारोह ('लिंक',' टेम्पलेट', आदि)। इसे अक्सर उदाहरणों में छोड़ा जाता है क्योंकि यह डिफ़ॉल्ट कार्रवाई है। – estus

6

मैं तकनीक का पालन करते हुए इस समस्या का समाधान। यह आपकी मदद कर सकता है।

खाका

<div data-ng-repeat="field in $ctrl.fields track by $index"> 
    <render-field data-field-type="{{field.type}}"></render-field> 
</div> 

एक घटक

/** 
* @ngdoc Component 
* @name app.component.renderField 
* @module app 
* 
* @description 
* A component to render Field by type 
* 
* @author Mohan Singh (gmail::[email protected], skype :: mohan.singh42) 
*/ 
(function() { 
    'use strict'; 

    angular 
    .module('app') 
    .component('renderField', { 
     bindings: { 
     fieldType: '@', 
     }, 
     template: '<div ng-include="$ctrl.templateUrl">', 
     controller: [ 
     function() { 
      var $ctrl = this; 
      $ctrl.$onInit = initialization; 
      $ctrl.$onDestroy = onDestroy; 
      $ctrl.$onChanges = onChanges; 

      /** 
      * public properties 
      */ 
      /** 
      * public methods 
      */ 
      /** 
      * @function 
      * @name initialization 
      * @description 
      * A component's lifeCycle hook which is called after all the controllers on an element have been constructed and had their bindings initialized 
      */ 
      function initialization() { 
      } 

      /** 
      * @function 
      * @name onChanges 
      * @description 
      * A component's lifeCycle hook which is called when bindings are updated. 
      */ 
      function onChanges(bindings) { 
      if(bindings.fieldType && bindings.fieldType.isFirstChange()){ 
       //$ctrl.fieldType['text' | 'textarea' | 'select' | 'radio'] 
       $ctrl.templateUrl = 'partials/fields/'+$ctrl.fieldType+'.html'; 
      } 
      } 
      /** 
      * @function 
      * @name onDestroy 
      * @description 
      * A component's lifeCycle hook which is called when is called on a controller when its containing scope is destroyed. 
      * Usefull to release external resources, watches and event handlers. 
      */ 
      function onDestroy() { } 
     }] 
    }); 
})(); 
+1

मेट, वह समाधान उत्कृष्ट है! मैंने HTML में हार्डकोड किए गए कॉन्फ़िगर करने योग्य मान के साथ अलग-अलग मार्गों के साथ हल किया है (क्योंकि दायरा बाध्यकारी के लिए उपलब्ध नहीं है)। आपका समाधान बहुत अच्छा है। इच्छा है कि मैं इसे और अधिक वोट दे सकता हूं! –

+0

@DanielMackay आपकी मदद करने में प्रसन्न हैं। –

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