我如何使用LiveReload与AngularJS templateURL

如何使用LiveReload和Grunt保存templateURL以重新加载?

angular.module('meshApp', [ 'ngSanitize', 'ngRoute' ]) .config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' }) .otherwise({ redirectTo: '/' }); }); 

我有一个玉文件views / main.jade,当我保存处理到.tmp / views / main.html,目前这个工程,我可以看到模板,但是当我保存LiveReload无法重新加载页面。

有什么办法可以让它起作用吗?

另外这里是一个链接到我的GruntFile incase它帮助: http : //jsfiddle.net/daimz/Te5Xc/

编辑————————-

当我写出最初的答案时,实际上并没有什么真正稳定的,所以我要对livereload做一些调整。 从那时起很多改变。 在这一刻(2017年初),我使用了浏览器同步和webpack,HMR。

编辑————————-

我把它用在了我的Angular / Ionic项目上。

因为我认为更多的人正在寻找这样的东西,我做了一个github回购: https : //github.com/stefanKuijers/live-templates

我的解决scheme使用了Martin Kool写的(check)的live.js。 我只是添加了一些代码。 这是如何工作的:你只需在live-templates.js中添加路由到你的路由器。 live-templates.js获取路由器路由并将它们添加到live.js心跳。

如何使用它: – 获取脚本和保存 – 将第27行的routerURLvariables更改为路由器的URL – 在需要实时重载的页面上包含脚本

让我知道或如何为你们工作!

干杯

我简化了我的Gruntfile.js可能会有所帮助:

 appPath:"", //your app path watch: { options: { livereload: 35729, debounceDelay: 500 }, gruntfile: { files: ['Gruntfile.js'] }, css: { //if you use less or sass,you can compile and copy here }, js: { files: ['<%= appPath %>/{scripts}/{,**/}*.js'], tasks: ['newer:all'] }, html: { files: ['<%= appPath %>/{views}/{,**/}*.html'], tasks: ['newer:all'] }, livereload: { options: { livereload: 35729, debounceDelay: 500 }, files: [ '<%= appPath %>/{,**/}*.html', '<%= appPath %>/styles/{,**/}*.css', '<%= appPath %>/images/{,**/}*.{png,jpg,jpeg,gif,webp,svg}' ] } } 

并运行在:

 grunt.registerTask('server', [ ..., 'watch' ]); 

也许试试这个中间件:

 var modRewrite = require('connect-modrewrite'); 

然后在连接上:

 connect: { options: { port: 9000, // Change this to '0.0.0.0' to access the server from outside. hostname: '0.0.0.0', livereload: 35729 }, livereload: { options: { open: true, base: [ '.tmp', '<%= yeoman.app %>' ], middleware: function(connect, options) { var middlewares = []; //Matches everything that does not contain a '.' (period) middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]'])); options.base.forEach(function(base) { middlewares.push(connect.static(base)); }); return middlewares; } } } 

你也应该安装modrewrite: npm install connect-modrewrite --save-dev

我只是在这里猜测。 我希望它有帮助。