Angular 1.5组件方法templateUrl + function

时间:2015-11-21 09:45:01

标签: javascript angularjs

我试图使用angular 1.5.0-beta.2的应用程序

制定指令'我有以下代码:

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的函数但是如何将它与templateUrl结合使用?

3 个答案:

答案 0 :(得分:20)

在1.5.0-beta.2中templateUrl可以是由注入器调用的函数。 $element中的$attrscomponent are injected以及 ... templateUrl: function ($element, $attrs) { // access to $element and $attrs ... return $attrs.uninterpolatedTemplateUrl; } 中的both template and templateUrl functions以及任何其他依赖项。

这意味着

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

可以改为。

答案 1 :(得分:6)

@estus解决方案为我工作,直到我弄清了我的脚本。 Uglified它给出了以下错误:

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

对我有用的解决方案是:

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

答案 2 :(得分: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::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() { }
        }]
    });
})();