简体   繁体   English

具有ng-messages-include的AngularJS ng-template

[英]AngularJS ng-template with ng-messages-include

I'm trying to create a script type of text/ng-template to put my error messages template into $templateCache. 我正在尝试创建一个脚本类型的text / ng-template,将我的错误消息模板放入$ templateCache。 I want this script file to be remote, in it's own file outside of the HTML. 我希望这个脚本文件是远程的,在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 the HTML I want to reference this template that I believe should be in $templateCache and I can access it by Id. 然后在HTML中我想引用这个模板,我认为应该在$ templateCache中,我可以通过Id访问它。

<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 the script inline with the HTML it works just fine but I want it to be in a remote file. 如果我把脚本与HTML内联,它可以正常工作,但我希望它在远程文件中。 When I move it to a remote file my HTML is unable to find my template error-messages. 当我将其移动到远程文件时,我的HTML无法找到我的模板错误消息。

This is my first Plunker I'm sharing. 这是我第一次分享的Plunker。 Please let me know if you can't get to it. 如果你不能达到它,请告诉我。 http://plnkr.co/edit/NgSm7piaECWBab1LOmp3?p=preview http://plnkr.co/edit/NgSm7piaECWBab1LOmp3?p=preview

Please check the plunker - http://plnkr.co/edit/luhUuZURCOeHSuEhi11x?p=info 请检查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); 
  })
})

Load the ng-messages-include template using $http in the run phase of the application and save it in the templateCache. 在应用程序的运行阶段使用$ http加载ng-messages-include模板并将其保存在templateCache中。

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

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