AngularJS ng message template with ng messages includes

I am trying to create a text / ng template type script to put my error message template in $ templateCache. I want this script file to be deleted, it has its own file outside of HTML.

<script type="text/ng-template" id="error-messages">
  <span ng-message="required">This field is required.</span>
  <span ng-message="min">This number must be larger than {{min}}.</span>
  <span ng-message="max">This number must be smaller than {{max}}.</span>
  <span ng-message="number">A number is required.</span>
  <span ng-message="date">A date is required.</span>
</script>

Then in HTML I want to reference this template, which I believe should be in $ templateCache, and I can access it by identifier.

<form name="userForm">       
      <div class="form-group">
        <label for="requiredInput">Required</label>
        <input type="text" class="form-control" id="required" name="requiredInput" 
        ng-model="user.required" required />
        <div ng-messages="userForm.requiredInput.$error" ng-messages-include="error-messages"></div>
      </div>
</form>

If I put a script embedded in HTML, it works fine, but I want it to be in a remote file. When I move it to a remote file, my HTML cannot find my template error messages.

This is my first Plunker I share. Please let me know if you cannot reach it. http://plnkr.co/edit/NgSm7piaECWBab1LOmp3?p=preview

+4
1

, plunker - http://plnkr.co/edit/luhUuZURCOeHSuEhi11x?p=info

angular.module('app', ['ngMessages'])
.run(function ($templateCache, $http) {
  $http.get('scriptTemplate.html')
  .then(function(response) {
    $templateCache.put('error-messages', response.data); 
  })
})

ng-messages-include $http templateCache.

+12

All Articles