Angular 1.5组件方法templateUrl + function

tuv*_*kki 19 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('')
  }
});
Run Code Online (Sandbox Code Playgroud)

我喜欢与访问返回函数模板的想法$element$attrs,但我怎么结合这与templateUrl?

Est*_*ask 23

在1.5.0-beta.2中templateUrl可以是由注入器调用的函数.$element$attrs 注入两个templatetemplateUrl功能component,以及任何其他依赖关系.

这意味着

  ...
  templateUrl: function ($element, $attrs) {
    // access to $element and $attrs
    ...
    return $attrs.uninterpolatedTemplateUrl;
  }
Run Code Online (Sandbox Code Playgroud)

可以改为做.


Bac*_*ker 7

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

Error: [$injector:unpr] Unknown provider: eProvider <- e
Run Code Online (Sandbox Code Playgroud)

对我有用的解决方案是:

['$element', '$attrs', function($element, $attrs) {
    return $attrs.uninterpolatedTemplateUrl;
}]
Run Code Online (Sandbox Code Playgroud)

  • **Angular中使用DI的所有**函数都应注释,以便适当缩小.即几乎Angular API中的每个函数,除了指令函数(`link`,`template`等).这在示例中经常被省略,因为这是默认操作. (3认同)

Moh*_*ngh 6

我通过以下技术解决了这个问题.这可能对你有所帮助.

模板

<div data-ng-repeat="field in $ctrl.fields track by $index">
  <render-field data-field-type="{{field.type}}"></render-field>
</div>
Run Code Online (Sandbox Code Playgroud)

一个组件

/**
 * @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() { }
        }]
    });
})();
Run Code Online (Sandbox Code Playgroud)