具有angular2中的函数的templateUrl [英] templateUrl with a function in angular2

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

问题描述

好的...在angular 1.xy中是

  angular.module('myApp',[])。directive ('myDirective',function(){
return {
templateUrl:function(tElement,iAttrs){
return'http://'+ iAttrs.myDirective //更多...
}
}
});

但是..在Angular2中



<$ p $ @Component({
选择器:'my-Directive',
templateUrl:'http:// ???'
})
class HelloWorld {
}

好的,在 doc 只表示一个字符串。因为它被处理为angular2中的函数吗?

解决方案

我必须实现类似的东西,我的解决方案与Thomas Gassmann的评论如上,所以我决定分享。

目前(角度为4.4.5)@Component装饰器只接受一个字符串,因此模板不像angularJS那样动态编译。但是,您可以实现多个组件并动态切换组件。示例如下:

https:// stackblitz.com/edit/angular-dynamic-templateurl


Well... in angular 1.x.y is

angular.module('myApp', []).directive('myDirective', function(){
    return {
       templateUrl : function(tElement, iAttrs){
           return 'http://' + iAttrs.myDirective // More...
       } 
    }
});

But.. In Angular2

@Component({
    selector: 'my-Directive',
    templateUrl: 'http://???' 
})
class HelloWorld {
}

Well, in the doc say only a String. As it is handled to be a function in angular2 ?

解决方案

I had to implement something similar and my solution was the same as Thomas Gassmann's comment above, so I decided to share.

Currently (angular 4.4.5) @Component decorator only accepts a string, therefore the template is not dynamically compiled like on angularJS. However you can implement multiple components and switch the component dynamically. Example below:

https://stackblitz.com/edit/angular-dynamic-templateurl

这篇关于具有angular2中的函数的templateUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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