加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

动态templateUrl – AngularJS

发布时间:2020-12-17 07:48:34 所属栏目:安全 来源:网络整理
导读:所以从角度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
所以从角度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;
};

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读