我如何使用不同的模板,用我的角度指令亭子包?亭子、用我、如何使用、指令

2023-09-13 03:06:25 作者:不說分手的愛情`

所以,我有一大套我创造,我想在很多项目中使用的指令,所以我把它变成一个凉亭包,并在我的项目之一,包括它。不幸的是,指令将无法正常工作,因为templateUrl路径不正确。

该templateUrls基于模板在同一目录中JS的指令之中。因此,./tabbedtextareas.html什么是我的一些简单的方式来解决这个问题呢?

我想过到目前为止的是:

复制并粘贴HTML到JS文件编辑模板将是一个痛苦之后 使用咕噜编译与JS的模板,甚至创造了钩承诺,合并掌握和推动。这是一个非常简单的插件,我preFER让事情尽可能的简单。 把我的模板目录,然后在每个项目中都有我的服务器处理的请求到该文件夹​​。任何人要求我的指令在凉亭的依赖就需要知道这个具体的事情。 IE浏览器。包不会安装简单地安装鲍尔

有可能像一个凉亭安装脚本,您可以添加什么?任何帮助是AP preciated。

解决方案

我一直在寻找指导/建议在这个非常的问题,并审议了一些不同的选择。我想我会分享我现在看中了。

N.B。的项目,这是正在使用的方法上仍处于起步阶段,因此所描述的方法是没有一成不变的方法。我也不会感到惊讶,如果我不得不去适应/改变它的未来。

背景环境是一样的,我们已经有多个自足指令生活在GitHub是通过亭子使用。每个组件的模板已成为行内的指令,如 templateUrl 路径是行不通的。

我基本上是在做选择2上面,使用一饮而尽,并使用一饮而尽-角templatecache插件利用角模板缓存。

解决方案

gulpfile.js

请问两件事情,解析组件名称和写模板内容模板缓存(通过咽插件)并连接组件code和标记为单个文件(到 DIST /<组分 - 名称>的.js

  VAR一饮而尽=要求('吞掉'),    模板=要求('一饮而尽-角templatecache'),    CONCAT =要求('一饮而尽-CONCAT'),    干净=要求('一口干净'),    PKG =要求('./的package.json');VAR模板='myTemplate.tpl.htmlgulp.task('模板',函数(){  返回gulp.src(模板)    .pipe(模板('templates.tmp',{      根:'/模板/',      模块:pkg.name    }))    .pipe(gulp.dest()'。');});gulp.task('CONCAT',['模板'],函数(){  返回gulp.src([pkg.main,'templates.tmp'])    .pipe(CONCAT(pkg.name +'的.js'))    .pipe(gulp.dest('./ DIST /'));});gulp.task('干净',['CONCAT'],函数(){  gulp.src('。./* TMP',{阅读:假})    .pipe(清洁());});gulp.task('手表',函数(){  gulp.watch(['* JS。','*的.html'],['构建']);});gulp.task('打造',['模板','CONCAT','干净'])​​;gulp.task('默认',['建设','手表']); 

输出

模板获取模板缓存设置,并且通过指令 $ templateCache.get(键)设置模板属性。这给使用通过凉亭这个组件,需要的单一文件输出,同时让您保持标记了在源一个单独的文件。

  angular.module(MyModule的,[])。指令(myDirective',函数($ templateCache){  返回{    模板:$ templateCache.get('/模板/ myTemplate.tpl.html');    链接:功能(范围,榆树,ATTR){    }  };});。angular.module(MyModule的),运行([$ templateCache功能($ templateCache){$ templateCache.put(/模板/ myTemplate.tpl.html,< D​​IV>这是模板您组分LT; / DIV>中);}]); 

的思考

文明村镇 盛家坝除了二官寨,这些地方也值得打卡

有有一个组件上工作时构建步骤的额外开销。鉴于要求,我不认为有一种方法可以完全避免这样的步骤。如果它不组件制作过程中进行,那么就不得不在执行时间内完成。鉴于这些2个选择,我认为这是好做它的组件时,范围窄,明确的。

我不相信我一饮而尽任务完全是最优的。有一定的同步性,这有悖于的一饮而尽的方式。也许可以弄清楚如何与一些时间/精力改善它。

So I have a large set of directives I created that I want to use on many projects, so I turned it into a bower package and included it in one of my projects. Unfortunately the directives won't work, because the templateUrl path is incorrect.

The templateUrls are based on the templates being in the same directory as the js for the directives. So "./tabbedtextareas.html" What are some of my simpler options for solving this problem?

The ones I've thought of so far are:

copy and paste the html into the JS files editing the templates will be a pain afterwards use grunt to compile the templates with the JS, perhaps even creating a hook to commit, merge to master and push. This is a very simple plugin and I prefer to keep things as simple as possible. put my templates in a directory, then in each project have my server handle requests to that folder. anyone requiring my directives as a dependency in bower would need to do know this specific thing. Ie. the package would not installable simply by bower install.

Is there maybe like a bower install script you can add or something? Any help is appreciated.

解决方案

I've been on the hunt for guidance/advice on this very problem, and have considered a number of different options. I thought I'd share what I've settled on currently.

N.B. the project that this is approach is being used on is still in its early stages, so the approach described is by no means set in stone. I wouldn't be surprised if I had to adapt/change it in the future.

Background context is the same, we've got multiple self-contained directives living on GitHub that are used via bower. Each components template has to be inline with the directive, as templateUrl paths won't work.

I'm essentially doing option 2 from above, using gulp, and leveraging the angular template cache using the gulp-angular-templatecache plugin.

Solution

gulpfile.js

Does 2 things, parse component name and write template contents to template cache (via gulp plugin) and concatenate component code and mark-up into single file (into dist/<component-name>.js.

var gulp = require('gulp'),
    templates = require('gulp-angular-templatecache'),
    concat = require('gulp-concat'),
    clean = require('gulp-clean'),
    pkg = require('./package.json');

var template = 'myTemplate.tpl.html'

gulp.task('templates', function () {
  return gulp.src(template)
    .pipe(templates('templates.tmp', {
      root: '/templates/',
      module: pkg.name
    }))
    .pipe(gulp.dest('.'));
});

gulp.task('concat', ['templates'], function () {
  return gulp.src([pkg.main, 'templates.tmp'])
    .pipe(concat(pkg.name + '.js'))
    .pipe(gulp.dest('./dist/'));
});

gulp.task('clean', ['concat'], function () {
  gulp.src('./*.tmp', {read: false})
    .pipe(clean());
});

gulp.task('watch', function () {
  gulp.watch(['*.js', '*.html'], ['build']);
});

gulp.task('build', ['templates', 'concat', 'clean']);
gulp.task('default', ['build', 'watch']);

Output

The template gets set in the template cache, and the directive retrieves it via $templateCache.get(key) when setting the template attribute. This gives the single file output needed to use this component via bower, whilst allowing you to maintain the mark up in a separate file in source.

angular.module('myModule', []).directive('myDirective', function ($templateCache) {

  return {
    template: $templateCache.get('/template/myTemplate.tpl.html');
    link: function (scope, elm, attr) {

    }
  };
});

angular.module("myModule").run(["$templateCache", function($templateCache) {$templateCache.put("/template/myTemplate.tpl.html","<div>This is the template for your component</div>");}]);

Thoughts

There's an additional overhead of having a build step when working on a component. Given the requirement, I don't think there's a way to completely avoid such a step. If it's not done during component build, then it'd have to be done at implementation time. Given those 2 options, I think it's better to do it in the component when the scope is narrow and clear.

I'm not convinced my gulp tasks are entirely optimal. There's a certain amount of synchronicity, which runs contrary to "the gulp way". Could probably figure out how to improve it with some time/effort.