【问题标题】:Angular 1.5 component method templateUrl + functionAngular 1.5 组件方法 templateUrl + 函数
【发布时间】:2016-02-23 20:09:04
【问题描述】:

我正在尝试让应用程序使用 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 结合起来?

【问题讨论】:

  • 你不要组合它们,你要么使用一个或另一个。
  • 我并不感到惊讶,但有点失望。谢谢 dfsq。
  • 为什么失望。您到底想对模板和模板 url 做什么?这就像要求电梯内的楼梯......他们是独一无二的......
  • Ehrm ...我从来没有这样想过(很好的类比),只是觉得有机会在单独的文件中拥有一个模板会很好也与 attr 和 element 混合...

标签: javascript angularjs


【解决方案1】:

在 1.5.0-beta.2 中,templateUrl 可以是注入器调用的函数。 $element$attrs are injectedboth template and templateUrl functions 中的 component,以及任何其他依赖项。

这意味着

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

可以代替。

【讨论】:

  • 这在使用外部模板时是如何工作的? templateUrl 通常包含对模板的引用。
  • 没错。因此,可以根据例如模板 url 做出决定。元素属性。你打算用它做什么?如果您希望在 templateUrl 函数中获得模板响应,那么不,您仍然需要稍后在指令中处理模板(我们在组件中没有链接功能,但有控制器)。
  • 我明白,estus,我在这里没有真正的目标。只是想知道。
  • 如何将绑定元素传递到 templateUrl 中,其中 templateUrl 是外部 html 文件的路径?
  • @JonHarding 不确定你的意思。请创建一个可以解释您的案例的问题。
【解决方案2】:

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

模板

<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() { }
        }]
    });
})();

【讨论】:

  • 伙伴,这个解决方案非常棒!我通过不同的路由解决了问题,在 HTML 中硬编码了可配置的值(因为范围不可用于绑定)。您的解决方案要好得多。希望我能投票更多!
  • @DanielMackay 很高兴为您提供帮助。
【解决方案3】:

@estus 解决方案对我有用,直到我丑化了我的脚本。 Uglified它给出了以下错误:

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

对我有用的解决方案是:

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

【讨论】:

  • Angular 中使用 DI 的 所有 函数应进行注释,以便正确缩小。 IE。 Angular API 中的几乎所有函数,除了指令函数(linktemplate 等)。这在示例中经常被省略,因为这是默认操作。
猜你喜欢
  • 1970-01-01
  • 2016-08-21
  • 2017-05-24
  • 1970-01-01
  • 2016-12-09
  • 2016-08-04
  • 2017-07-27
  • 2017-08-04
  • 1970-01-01
相关资源
最近更新 更多