2016-08-04 63 views
1

我想創建一個角度表單生成器。我有模板是這樣的:如何使用Angular函數導出Angular HTML模板

<script type="text/ng-template" id="textarea.html"> 
    <div class="items form-group"> 
     <label class="col-sm-2 control-label"> 
      {{item.title}} 
     </label> 
     <div class="col-sm-10"> 
      <textarea ng-model="task.title" placeholder="{{item.placeholder}}" class="form-control"></textarea> 
     </div> 
    </div> 
</script> 

,當用戶添加該模板生成HTML角這樣的代碼:

<div class="items form-group"> 
     <label class="col-sm-2 control-label"> 
      {{item.title}} 
     </label> 
     <div class="col-sm-10"> 
      <textarea ng-model="task.title" placeholder="{{item.placeholder}}" class="form-control"></textarea> 
     </div> 
    </div> 

有了這個例子中我的模板代碼渲染,但我不希望使其與只顯示有角度的html代碼。如果您有任何建議,請更改此代碼。

Example Code

回答

1

可以使用NG綁定,而不是NG-包括;

例如:

**in app.js.** 

app.run(['$injector', function($injector) { 
    var rootScope = $injector.get('$rootScope'); 

    rootScope.tpl = '<h1 style="color:green;">This is the page content</h1>'; 
}]) 

**in index.js** 

<span ng-bind='tpl'></span> 
+0

如何模板分配到'$ injector'。我有很多包含這個名稱的模板,不能放入'app.run(....)'。 –

+0

你可以使用$ templateCache服務來獲取模板,這是script指令的內容 – Qiu

0

如果使用普通的標籤,它將使但如果你不想使其那麼你可以使用

replace < by &lt; and > by &gt;