首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular基础指令templateUrl属性

Angular基础指令templateUrl属性
EN

Stack Overflow用户
提问于 2014-12-23 05:05:12
回答 3查看 732关注 0票数 0

我正在使用angular-foundation (http://pineconellc.github.io/angular-foundation/),它利用angular指令来实现像Modals、Tabs和其他前端服务这样的基础功能。

我遇到的问题是,所有指令都有预先填充的"templateUrl“属性,而这些属性在我的服务器上并不存在。另外,因为这是由Bower管理的外部依赖项,所以我不能手动更新库。我从库中提取了以下指令:

代码语言:javascript
复制
angular.module('mm.foundation.tabs', [])
    .directive('tabset', function() {
      return {
        restrict: 'EA',
        transclude: true,
        replace: true,
        scope: {},
        controller: 'TabsetController',
        templateUrl: 'template/tabs/tabset.html',
        link: function(scope, element, attrs) {
          scope.vertical = angular.isDefined(attrs.vertical) ? scope.$parent.$eval(attrs.vertical) : false;
          scope.justified = angular.isDefined(attrs.justified) ? scope.$parent.$eval(attrs.justified) : false;
          scope.type = angular.isDefined(attrs.type) ? scope.$parent.$eval(attrs.type) : 'tabs';
        }
      };
    })

从我的模块中,我声明了对mm.foundation模块的依赖:

代码语言:javascript
复制
angular.module('foundationDemoApp', ['mm.foundation.tabs']).controller('TabsDemoCtrl', function ($scope) {
  $scope.tabs = [
    { title:"Dynamic Title 1", content:"Dynamic content 1" },
    { title:"Dynamic Title 2", content:"Dynamic content 2" }
  ];

  $scope.alertMe = function() {
    setTimeout(function() {
      alert("You've selected the alert tab!");
    });
  };
})

我目前正在使用Gulp和gulp-html2js来捆绑我的模板。在github自述文件中,他们提到了定制模板(https://github.com/pineconellc/angular-foundation)的一些关于$templateCache的东西,但在这一点上,我不确定如何将库中定义的templateUrl更改为我的模板位置。

任何关于这方面的指导都将不胜感激!

EN

回答 3

Stack Overflow用户

发布于 2014-12-23 05:40:19

既然没有人回答,我会试一试,它可能会对你有帮助,ossys (请任何版主注意,如果我错了,请随意删除我的答案)。

如果你bower install了这个库,那么这些模板应该在bower_components/angular-foundation中的某个地方,你应该按照他们的示例https://github.com/pineconellc/angular-foundation#customize-templates来做,并得到如下内容:

代码语言:javascript
复制
html2js: {
  options: {
    base: '.',
    module: 'ui-templates',
    rename: function (modulePath) {
      var moduleName = modulePath.replace('bower_components/angular-foundation', '').replace('.html', '');
      return 'template' + '/' + moduleName + '.html';
    }
  },
  main: {
    src: ['bower_components/angular-foundation/**/*.html'],
    dest: '.tmp/ui-templates.js'
  }
}
//or in gulp
gulp.src('bower_components/angular-foundation/**/*.html')
  .pipe(html2js({
    outputModuleName: 'ui-templates'
  }))
  .pipe(concat('ui-templates.js'))
  .pipe(gulp.dest('./'))
票数 1
EN

Stack Overflow用户

发布于 2014-12-23 05:59:41

使用$templateCache,我认为可以像这样用您自己的模板替换模板

对于选项卡:

<script type="text/ng-template" id="template/tabs/tabset.html" src="/path/to/your/template.html"></script>

票数 1
EN

Stack Overflow用户

发布于 2014-12-23 09:11:35

嘿,谢谢你们的回答,实际上你们两个都是对的。

我的问题是我在反向思考解决方案。在我的例子中,我需要重命名我正在编译的模板的名称,而不是替换每个指令的templateUrl参数。例如..。

我使用gulp和html2js来捆绑我的模板文件。在导入angular-foundation之前,我的模板任务看起来像这样:

代码语言:javascript
复制
gulp.task('templates', function (cb) {

           gulp.src('path/to/tempalates/**/*.html')
             .pipe(html2js({
               outputModuleName: 'my.templates',
               base: './src/web/',
             }))
             .pipe(concat('templates.js'))
             .pipe(insert.prepend("var angular = require('angular');"))
             .pipe(gulp.dest('./src/web/'))
             .on('end', done);
         };
});

我继续从angular-foundation github存储库上传html模板目录,并将其放在路径' path /to/foundation/ template‘中。然后,我更新了templates gulp任务,以便它将“path / to /foundation/template”路径重命名为已经在指令中编码的路径。例如,如果指令预期的路径为‘templateUrl /tab/tabs.html’,而实际模板的位置是' path / to /foundation/ template /tab/tabs.html',则重命名函数会在构建期间将'path/to/foundation‘替换为空字符串,以使路径为'template/tab/tabs.html’。我最后的任务现在看起来是这样的:

代码语言:javascript
复制
gulp.task('templates', function (cb) {
           gulp.src('path/to/tempalates/**/*.html','path/to/foundation/template/**/*.html'])
             .pipe(html2js({
               outputModuleName: 'my.templates',
               base: './src/web/',
               rename : function (modulePath) {
                  var moduleName = modulePath.replace('path/to/foundation/', '').replace('.html', '');
                  return moduleName + '.html';
                }
             }))
             .pipe(concat('templates.js'))
             .pipe(insert.prepend("var angular = require('angular');"))
             .pipe(gulp.dest('./src/web/'))
             .on('end', done);
         };
});

因此,现在我的所有模板都是使用angular-foundation期望它们所在的路径构建的,并且我不需要在运行时管理模板路径。

我希望这对其他人有帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27610395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档