无法动态设置ng-model

时间:2019-05-14 14:55:32

标签: javascript angularjs

我想将数据输入到ArrayINeed中,因为这是我要传递回API调用的对象。目前,我对"ConfirmedTrackingReferenceNumbers": Dc.ArrayINeed不确定。我已经在线和在SO上阅读了多个帖子,但是无法解决该问题。

控制器

.
.
.
var Dc = this;
.
.
.
Dc.FlowerData = function () {
    Dc.ArrayINeed = [];
    if (Dc.Data.validate()) {
        if (isTulipFlower) {
            requestData = {
                "FlowerId": Dc.FlowerId,
                "ConfirmedTrackingReferenceNumbers": Dc.ArrayINeed
                };
.
.
.

HTML

<div ng-repeat="(item, data) in Dc.DataToLoopThrough">
    <div ng-if="item === 'flowers'">
        <div ng-repeat="flowerItem in data">
            <table ng-if="flowerItem.flowerType === 'Tulip'">
                <tr ng-repeat="refNumber in flowerItem.flowerReferenceNumbers">
                    <td ng-if="refNumber.isRequiredForTracking && refNumber.value != null">
                        <input ng-value="refNumber.value != null ? refNumber.value : ''" type="text" />
                    </td>
                    <td ng-if="refNumber.isRequiredForTracking && refNumber.value == null">
                        <input ng-model="Dc.ArrayINeed[$index].refNumber" type="text" />
                    </td>       
                </tr>
            </table>

1 个答案:

答案 0 :(得分:0)

请勿同时使用ng-valueng-model

  <table>
    <tr ng-repeat="refNumber in flowerItem.flowerReferenceNumbers">
        <td class="requirement-td-padding"
            ng-if="refNumber.isSetForTrimming">
            {{refNumber.label}}: &nbsp;
        </td>
        <td ng-if="refNumber.isSetForTrimming">
            <input ̶n̶g̶-̶v̶a̶l̶u̶e̶=̶"̶r̶e̶f̶N̶u̶m̶b̶e̶r̶.̶v̶a̶l̶u̶e̶ ̶!̶=̶ ̶n̶u̶l̶l̶ ̶?̶ ̶r̶e̶f̶N̶u̶m̶b̶e̶r̶.̶v̶a̶l̶u̶e̶ ̶:̶ ̶'̶'̶"̶ ̶
                   ng-model="Dc.ArrayINeed[$index]" type="text" />
        </td>
    </tr>
  </table>

相反,在控制器中初始化模型:

Dc.FlowerData = function () {
    var flowerArr = Dc.DataToLoopThrough.flowers;
    var flowerItem = flowerArr.find(_ => _.flowerType == 'Tulip');
    var refNumberArr = flowerItem.flowerReferenceNumbers;
    Dc.ArrayINeed = refNumberArr.map(_ => (_.value || ''));

    //....
};