微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动态templateUrl – AngularJS

所以从角度1.1.4,你可以有一个动态的模板网址.从 here,

templateUrl – Same as template but the template is loaded from the specified URL. Because the template loading is asynchronous the compilation/linking is suspended until the template is loaded.

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”

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'
  }
}

鉴于我无法访问该范围,我该如何管理?

在AngularJS中也可以创建动态模板:
在您的指令中使用:
template : '<div ng-include="getTemplateUrl()"></div>'

现在您的控制器可能会决定使用哪个模板:

$scope.getTemplateUrl = function() {
  return '/template/angular/search';
};

由于您可以访问范围参数,您还可以执行以下操作:

$scope.getTemplateUrl = function() {
  return '/template/angular/search/' + $scope.query;
};

所以您的服务器可以为您创建一个动态模板.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐