WearyMonkey / ngtemplate-loader

Include AngularJS templates in the Webpack bundle and preload the template cache.
MIT License
238 stars 78 forks source link

Use with ng-include, ng-messages-include & friends? #53

Closed ClementParis016 closed 8 years ago

ClementParis016 commented 8 years ago

I was wondering what would be the good way to handle templates with ng-include or ng-messages-include.

For example, the following code will fail as it tries to load the template without hitting $templateCache:

<div ng-messages-include="error-messages.html"></div>

I've tried a few things, like writing import errorMessageTemplate from './error-messages.html in my main module, but it doesn't work :( Any suggestions?

ClementParis016 commented 8 years ago

Pushing my investigation further, I came across #17 which led me to the following working solution:

/* my-form.controller.js */

import errorMessageTemplatePath from './error-messages.html';

class MyFormController {
  constructor() {
    this.errorMessageTemplatePath = errorMessageTemplatePath;
  }
  // ...
}
<!-- my-form.html -->

<!-- ... -->
<div  ng-messages="myForm.myField.$error">
  <div ng-messages-include="{{$ctrl.errorMessageTemplatePath}}"></div>
</div>
<!-- ... -->

It still feels a bit hack-ish but looking at how this loader works it makes complete sense.