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

时间:2016-08-23 11:28:08

标签: javascript html angularjs templates render

我有一个这样的对象:

$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>

1 个答案:

答案 0 :(得分:1)

您可以将模板包含在:

<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>