7

Я использую angularUI typehead для элемента ввода. Я использую настраиваемый шаблон для отображения значений. Все работает нормально. Мой вопрос заключается в том, как разделить ng-template на отдельный файл, чтобы его можно было повторно использовать и в других файлах.Как отделить ng-шаблон в отдельном файле

В случае мои слова не ясно, пожалуйста, обратите внимание, что я имею в виду ng-templates конкретно и не обеспокоены отдельного другого содержимого HTML

Вот код:

// custom template begin 
// this is the ng-template 
// I'd like to move this custom template into to another file 
// similar to partials 
<script type="text/ng-template" id="customTemplate.html"> 
    <a> 
     <b>{{match.model.name}}</b> 
     <div>{{match.model.username}}</div> 
    </a> 
</script> 
//custom template end 


// input element makes use of the ng-template defined above 
<div class="form-group"> 
<label class="col-md-2 control-label normal" for="assignTo">Assign To</label> 
<div class="col-md-3"> 
    <input name="bug_assignTo" id="bug_assignTo" ng-model="bug.assignTo" typeahead="user.username as user.name for user in config.users | filter:$viewValue | limitTo:8" class="form-control input-sm" typeahead-on-select="bug.assignTo = $item" placeholder="type name" typeahead-template-url="customTemplate.html"></input> 
</div> 

Собираем в частично не работает, например: <div ng-include="app/client/bug/new/my-ng-template.partial.html"></div> бросает:

Tried to load angular more than once. 
[$rootScope:infdig] 10 $digest() iterations reached. Aborting! 
... 
.... 

ответ

11

Вам не нужно ng-include.

Перемещение шаблона в отдельный файл, позволяет сказать, что имя файла customTemplate.html и содержимое файла как

<a> 
    <b>{{match.model.name}}</b> 
    <div>{{match.model.username}}</div> 
</a> 

Не включайте <script type="text/ng-template" id="customTemplate.html"> тег, когда он перемещается в отдельный файл.

Используйте правильный путь к файлу, как

<input name="bug_assignTo" 
       id="bug_assignTo" 
       ng-model="bug.assignTo" 
       typeahead="user.username as user.name for user in config.users | filter:$viewValue | limitTo:8" 
       class="form-control input-sm" 
       typeahead-on-select="bug.assignTo = $item" 
       placeholder="type name" 
       typeahead-template-url="customTemplate.html" /> <!--put correct physical path here, if it is inside partial folder then use partial/customTemplate.html--> 
+0

Спасибо, что работали. – Sudhakar

+0

Возможно, это глупый вопрос, но использует ли шаблон шаблон templateCache, например, когда он не находится в теге Script с типом = «текст/шаблон»? –

+0

Да, вы можете использовать $ templateCache – Reza

Смежные вопросы