Angular 1.5 component method templateUrl + function

后端 未结 3 1676
温柔的废话
温柔的废话 2020-11-30 11:00

I\'m trying to get an app working with angular 1.5.0-beta.2

To make a \'directive\' I have the following code:

myApp.component(\'gridshow\', {
  bind         


        
相关标签:
3条回答
  • 2020-11-30 11:38

    I solved this problem by following technique. This may help you.

    Template

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

    A component

    /**
     * @ngdoc Component
     * @name app.component.renderField
     * @module app
     *
     * @description
     * A component to render Field by type
     *
     * @author Mohan Singh ( gmail::mslogicmaster@gmail.com, 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() { }
            }]
        });
    })();
    
    0 讨论(0)
  • 2020-11-30 11:49

    @estus solution worked for me until I uglified my scripts. Uglified it gave the following error:

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

    The solution that worked for me is:

    ['$element', '$attrs', function($element, $attrs) {
        return $attrs.uninterpolatedTemplateUrl;
    }]
    
    0 讨论(0)
  • 2020-11-30 11:56

    In 1.5.0-beta.2 templateUrl can be a function that is invoked by injector. $element and $attrs are injected into both template and templateUrl functions in component, as well as any other dependencies.

    This means that

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

    can be done instead.

    0 讨论(0)
提交回复
热议问题