简体   繁体   English

如何将LiveReload与AngularJS templateURL一起使用

[英]How can I use LiveReload with an AngularJS templateURL

How can I get the templateURL to reload when saved using LiveReload and Grunt? 如何使用LiveReload和Grunt保存时重新加载templateURL?

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

I have a jade file views/main.jade that when I save is processed to .tmp/views/main.html, currently this works and I can see the template, however when I save LiveReload is unable reload the page. 我有一个jade文件views / main.jade,当我保存处理到.tmp / views / main.html时,目前这个工作,我可以看到模板,但是当我保存LiveReload无法重新加载页面时。

Is there any way I can get it to work? 有什么方法可以让它起作用吗?

Also here is a link to my GruntFile incase it helps: http://jsfiddle.net/daimz/Te5Xc/ 这里还有一个链接到我的GruntFile,它有助于: http//jsfiddle.net/daimz/Te5Xc/

EDIT ------------------------- 编辑-------------------------

When I wrote the initial answer there was not really anything really stable enough and thats why I make some adjustments to livereload. 当我写下初始答案时,实际上并没有真正足够稳定的东西,这就是我为什么要对livereload进行一些调整的原因。 Since that time a lot changed. 从那时起,很多人都改变了。 At this moment (early 2017) I use browser-sync & webpack, HMR. 此时(2017年初)我使用浏览器同步和网络包,HMR。

EDIT ------------------------- 编辑-------------------------

I got it to work on my Angular/Ionic project. 我让它在我的Angular / Ionic项目上工作。

As I assume that more people are looking for something like it I made a github repo: https://github.com/stefanKuijers/live-templates 我假设有更多的人在寻找类似的东西我做了一个github回购: https//github.com/stefanKuijers/live-templates

My solution uses live.js which Martin Kool wrote (check ). 我的解决方案使用了Martin Kool写的live.js(检查)。 I just added some code. 我刚刚添加了一些代码。 This is how it works: You just add the path to your router in live-templates.js. 这是它的工作原理:您只需在live-templates.js中添加路由器的路径。 The live-templates.js gets the routers routes and adds them to the live.js heartbeat. live-templates.js获取路由器路由并将它们添加到live.js心跳。

How to use it: - get script & save - change the routerURL variable on line 27 to the url of your router - include script on the page(s) where you require live reload 如何使用它: - 获取脚本并保存 - 将第27行的routerURL变量更改为路由器的URL - 在需要实时重新加载的页面上包含脚本

Let me know or and how it worked for you guys! 让我知道或者它对你们有用吗!

Cheers 干杯

I simplified my Gruntfile.js may helpful: 我简化了我的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}'
        ]
    }
}

and run in : 并运行:

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

Maybe try this middleware: 也许试试这个中间件:

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

Then on the connect: 然后在连接上:

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

You should also install modrewrite: npm install connect-modrewrite --save-dev 你还应该安装modrewrite: npm install connect-modrewrite --save-dev

I am only guessing here. 我只在这里猜测。 I hope it helps. 我希望它有所帮助。

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

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