我们可以将ng-model分配给span吗?

时间:2017-12-05 10:47:15

标签: javascript angularjs

我需要根据下拉列表中选定的答案将值分配给span。

screenshot

<tr class="tableheightQues" ng-repeat="question in questionnaireData track by $index">
<td>{{$index + 1}}) </td>
<td style="width:50%;"><span class="" title="{{question.QuestionItem}}">{{question.QuestionItem}}</span> </td>
<td>
    <div class="">
        <select ng-if="question.MasterDataCategoryId != null"
                id="answer{{question.QuestionId}}"
                ng-model="question.MasterData"
                ng-init="question.MasterData" class="form-control"
                style="width: 300px; white-space:pre-wrap;">
            <option ng-repeat="answer in answerData[question.MasterDataCategoryId]"
                    value="{{answer.MasterData}}">
              {{answer.MasterData}}
            </option>
        </select>
        <input ng-if="question.MasterDataCategoryId == null" 
               id="{{question.QuestionId}}" ng-model="question.MasterData"
               type="text" style="width: 300px;" name="question.QuestionId"
               class="form-control">
        <!--<span class="help-inline" ng-show="submitted && formVHD.$valid"></span>-->
    </div>
</td>
<td>
    <div class=""> 
      <span ng-if="question.MasterDataCategoryId != null"
            ng-repeat="answer in answerData[question.MasterDataCategoryId] | filter : question.MasterData : true">
        <span ng-if="question.MasterData" ng-model="question.Score">
        {{answer.Score}}
        </span>
        <span ng-if="!question.MasterData"> </span>
      </span>
      <select ng-if="question.MasterDataCategoryId == null"
              id="projectlist" class="form-control"
              required ng-model="question.Score">
            <option selected="selected"></option>
            <option ng-repeat="score in scoringDetails"
                    value="{{score.Score}}"> {{score.Score}}
            </option>
      </select>
    </div>
</td>
</tr>

以上是我使用的代码。

如何在保存所有信息的同时将ng-model分配给span show获取数据?

2 个答案:

答案 0 :(得分:0)

ng-model 指令适用于checkboxinput,电台等输入元素。

在您的情况下,您应该使用ng-bind之类的

如果您想要双向数据绑定,请使用

 <span ng-if="question.MasterData" ng-bind="question.Score"></span>

OR

 <span ng-if="question.MasterData">{{question.Score}}</span>

如果您正在寻找单向数据绑定,请在绑定前使用::

 <span ng-if="question.MasterData">{{::question.Score}}</span>

答案 1 :(得分:0)

ng-model指令仅适用于输入元素。您可以使用ng-bind代替

<span ng-if="question.MasterData" ng-bind="question.Score">

或使用花括号

<span ng-if="question.MasterData">{{question.Score}}</span>

详细了解指令here