问题描述
因此,作为角1.1.4,你可以有一个动态模板URL。从,
So as of Angular 1.1.4, you can have a dynamic template url. From here,
templateUrl - 同模板,但模板是从指定的URL装入。因为模板加载是异步的,直到模板被加载编译/链接暂停。
您可以指定templateUrl为一个字符串再presenting的URL或采用两个参数tElement和tAttrs(下编译函数API描述)并返回一个字符串值,再presenting链接的功能。
You can specify templateUrl as a string representing the URL or as a function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value representing the url.
我怎样才能利用该生成的基础上,比方说一个动态模板,我的指令属性?显然,这是不行的,因为tAttrs.templateType简直是字符串templateType
How can I utilize this to generate a dynamic template based on, say, an attribute on my directive? Obviously this doesn't work, since tAttrs.templateType is simply the string "templateType"
templateUrl: function (tElement, tAttrs) {
if (tAttrs.templateType == 'search') {
return '/b/js/vendor/angular-ui/template/typeahead/typeahead.html'
} else {
return '/b/js/vendor/angular-ui/template/typeahead/typeahead2.html'
}
}
由于我没有访问的范围,我该如何管理呢?
Given that I don't have access to the scope, how do I manage this?
推荐答案
下面也有可能在AngularJS创建动态模板:
在你使用的指令:
The following is also possible for creating dynamic templates in AngularJS:In your directive use:
template : '<div ng-include="getTemplateUrl()"></div>'
现在您的控制器可以决定要使用的模板:
Now your controller may decide which template to use:
$scope.getTemplateUrl = function() {
return '/template/angular/search';
};
由于您可以访问您的范围参数,你也可以这样做:
Because you have access to your scope parameters, you could also do:
$scope.getTemplateUrl = function() {
return '/template/angular/search/' + $scope.query;
};
所以,你的服务器可以为您创建一个动态模板。
So your server could create a dynamic template for you.
这篇关于动态templateUrl - AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!