如何将html表单模板编译成html字符串

How to compile html form template to html string

本文关键字:html 编译 字符串 表单      更新时间:2023-09-26

我有一个像这样的对象:

$scope.item ={
        fieldName:'First Name',
        fieldModel:'user.firstname',
        placeholder:'enter your name'
    }
我想编译这个html表单模板,像这样:
<script type="text/ng-template" id="input.html">
    <div class="items form-group">
        <label class="col-sm-2 control-label">
            {{item.fieldName}}
        </label>
        <div class="col-sm-10">
            <input type="text" ng-model="{{item.fieldModel}}" placeholder="{{item.placeholder}}" class="form-control">
        </div>
    </div>
</script>

到使用纯HTML设计的HTML字符串:

<div class="items form-group">
     <label class="col-sm-2 control-label">
         First Name
     </label>
     <div class="col-sm-10">
         <input type="text" ng-model="user.firstname" placeholder="enter your name" class="form-control">
     </div>
</div>

你可以用:

<div id="tpl-content" ng-include src="input.html"></div>

但是你的模板应该是:

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