我如何传递一个范围的变量为一个指令的`tAttrrs`对象吗? [英] How can I pass a scope variable into a directive's `tAttrrs` object?

查看:75
本文介绍了我如何传递一个范围的变量为一个指令的`tAttrrs`对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我想在动态生成的模板URL使用范围的变量。所以,我想这样的:

Alright, so I would like to use a scope variable in a dynamically generated template url. So I tried this:

HTML

<my-directive type="{{ type }}"></my-directive>

JS

angular.module('myApp', [])
  .directive('myDirective', function () {
    return {
      templateUrl: function (tElement, tAttrs) {
        return 'templates/myDirective.' + tAttrs.type + '.html';
      };
    };
  });

我期待 tAttrs.type 返回 $ scope.type 的价值,而是我结束了让 {{}类型} 。这导致了模板/ myDirective的templateUrl。{{类型}}。HTML

I was expecting tAttrs.type to return the value of $scope.type, but instead I ended up getting {{ type }}. This resulted in a templateUrl of templates/myDirective.{{ type }}.html.

所以,我能做些什么,以获得范围变量的值,而不是原始文本的?

So, what can I do to get the scope variable's value instead of the raw text?

推荐答案

范围值是不是从一个指令的templateUrl中访问。属性尚未编译,所以从这个范围内,不可能访问的范围。

Scope values are not accessible from within a directive's templateUrl. The attributes are not compiled yet, so from within this context it is not possible to access the scope.

下面是一个可能为你工作一个解决办法。

Here is a workaround that might work for you.

见Plunkr

我在这里所做的是使用含有一个div模板什么NG-包括,通过双向绑定获取的URL。

What I did here is using a template containing a div with ng-include, getting the url through a two-way bind.

这篇关于我如何传递一个范围的变量为一个指令的`tAttrrs`对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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