简体   繁体   English

Angular Foundation Directives templateUrl属性

[英]Angular Foundation Directives templateUrl Property

I'm using angular-foundation ( http://pineconellc.github.io/angular-foundation/ ) that leverages angular directives for implementing Foundation things like Modals, Tabs and other front-end services. 我使用的是angular-foundation( http://pineconellc.github.io/angular-foundation/ ),该方法利用angular指令来实现诸如Modals,Tabs和其他前端服务之类的Foundation东西。

The problem I'm having is that all of the directives have pre-populated "templateUrl" attributes that do not exist on my server. 我遇到的问题是,所有指令都具有预先填充的“ templateUrl”属性,这些属性在我的服务器上不存在。 Also, because this is an external dependency managed with Bower, I can't manually update the library. 另外,由于这是由Bower管理的外部依赖项,因此无法手动更新该库。 I pulled out the following directive from the library: 我从库中提取了以下指令:

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

from my module I claim a dependency on the mm.foundation module: 从我的模块中,我声明对mm.foundation模块的依赖:

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!");
    });
  };
})

I'm currently using Gulp with gulp-html2js to bundle my templates. 我目前正在使用Gulp和gulp-html2js捆绑模板。 On the github README they mention customizing templates ( https://github.com/pineconellc/angular-foundation ) something about the $templateCache, but at this point I am not sure how to change the templateUrl defined in the library to my template's location. 在github自述文件中,他们提到了自定义模板( https://github.com/pineconellc/angular-foundation )关于$ templateCache的内容,但是目前我不确定如何将库中定义的templateUrl更改为模板的位置。

Any guidance on this would be greatly appreciated! 任何对此的指导将不胜感激!

Since no one answered, I'll give it a shot, it'll may help you, ossys (note to any moderator, feel free to remove my answer if I'm wrong). 既然没有人回答,我会给您一个机会,它可能会对您有帮助,ossys(请注意任何主持人,如果我错了,请随时删除我的答案)。

If you bower install ed the library, those templates should be somewhere in bower_components/angular-foundation , you should follow their example https://github.com/pineconellc/angular-foundation#customize-templates and get something like : 如果您通过bower install该库,则这些模板应位于bower_components/angular-foundation某个bower_components/angular-foundation ,您应遵循其示例https://github.com/pineconellc/angular-foundation#customize-templates并获得类似以下内容的内容:

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('./'))

With $templateCache i think it is possible like this to replace the template with ur own 我认为使用$ templateCache可以用您自己的模板替换模板

for tab: 对于标签:

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

Hey thank you guys for your answer and actually you both are correct. 嘿,谢谢你们的回答,实际上你们俩都是正确的。

My problem is I was thinking of the solution backwards. 我的问题是我在想解决方案。 Rather than replacing the templateUrl parameter for each directive, in my case I instead need to rename the name of the template I'm compiling. 在我的情况下,我不需要重命名每个指令的templateUrl参数,而是需要重命名正在编译的模板的名称。 For example.... 例如....

I am using gulp and html2js to bundle my template files. 我正在使用gulp和html2js捆绑我的模板文件。 Before importing angular-foundation my templates task looked something like this: 在导入角度基础之前,我的模板任务如下所示:

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);
         };
});

I went ahead and uploaded the html template directory from angular-foundation github repository, and put that in the path 'path/to/foundation/template'. 我继续并从angular-foundation github存储库上传了html模板目录,并将其放在路径“ path / to / foundation / template”中。 I then updated the templates gulp task so that it would rename the 'path/to/foundation/template' path to the path that is already coded in the directive. 然后,我更新了模板gulp任务,以便将“ path / to / foundation / template”路径重命名为指令中已编码的路径。 For example, if a directive is expecting a templateUrl of 'template/tab/tabs.html' and the location of the actual template is 'path/to/foundation/template/tab/tabs.html', then the rename function would replace 'path/to/foundation' with an empty string to make the path 'template/tab/tabs.html' during the build. 例如,如果指令期望templateUrl为“ template / tab / tabs.html”,而实际模板的位置为“ path / to / foundation / template / tab / tabs.html”,则重命名函数将替换“ path / to / foundation”(带有空字符串),以在构建期间生成路径“ template / tab / tabs.html”。 My final gulp task now looks like: 我最后的任务是这样的:

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);
         };
});

So now all my templates are built with the path that angular-foundation expects them to be at, and I'm not stuck trying to manage template paths during run time. 因此,现在我所有的模板都是使用angular-foundation期望它们所位于的路径构建的,并且我在运行时尝试管理模板路径时也不会遇到麻烦。

I hope this helps others! 希望这对其他人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM