Angular 1.5 组件方法 templateUrl + 函数 [英] Angular 1.5 component method templateUrl + function

查看:26
本文介绍了Angular 1.5 组件方法 templateUrl + 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让应用程序使用 angular 1.5.0-beta.2

要制定指令",我有以下代码:

myApp.component('gridshow', {绑定:{幻灯片:'='},控制器:函数(){},控制器为:'网格',模板:函数($element,$attrs){//访问 $element 和 $attrs返回 ['

','<div ng-repeat="slide in grid.slides">','{{slide.image}}','</div>','</div>'].加入('')}});

我喜欢模板的想法,它返回一个可以访问 $element$attrs 的函数,但我如何将它与 templateUrl 结合起来?

解决方案

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

这意味着

<代码> ...templateUrl: 函数 ($element, $attrs) {//访问 $element 和 $attrs...返回 $attrs.uninterpolatedTemplateUrl;}

可以代替.

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

I like the idea of the template that returns a function with access to $element and $attrs but how do I combine this with a templateUrl?

解决方案

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.

这篇关于Angular 1.5 组件方法 templateUrl + 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆