更改ng-repeat循环内的ng-model

时间:2018-05-16 00:14:35

标签: javascript angularjs angular-ngmodel ng-bind

我正在尝试将ng-model绑定到基于ng-repeat的$ index或我正在循环的对象的键上的输入。

我试过这个

<div data-ng-repeat="(key, n) in langInput.values">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 u-no-padding">
        <label class="sell__label" for="auction_name_account_{{n.selected }}">Główna nazwa Twojej aukcji ({{n.selected }}):</label>
        <div class="pst-relative">
            <input type="text"
                   id="auction_name_account_{{n.selected }}"
                   class="form-control"
                   name="auction_name_account"
                   data-ng-model="inputs.auction_name_account[$index]"
                   data-ng-minlength="10"
                   data-ng-maxlength="60"
                   required />
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.required">Wymagane!</span>
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.minlength">Za krótkie!</span>
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.maxlength">Za długie!</span>
        </div>
    </div>
</div>

并且喜欢这个

<div data-ng-repeat="(key, n) in langInput.values">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 u-no-padding">
        <label class="sell__label" for="auction_name_account_{{n.selected }}">Główna nazwa Twojej aukcji ({{n.selected }}):</label>
        <div class="pst-relative">
            <input type="text"
                   id="auction_name_account_{{n.selected }}"
                   class="form-control"
                   name="auction_name_account"
                   data-ng-model="inputs.auction_name_account[key]"
                   data-ng-minlength="10"
                   data-ng-maxlength="60"
                   required />
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.required">Wymagane!</span>
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.minlength">Za krótkie!</span>
            <span class="sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account.$error.maxlength">Za długie!</span>
        </div>
    </div>
</div>

我正在循环的对象是:

$scope.langInput = {
    values: [
        {
            id: "1",
            selected: "pl"
        },
        {
            id: "2",
            selected: "eng"
        }
    ],

结果是我的ng模型inputs.auction_name_account[$index]inputs.auction_name_account[key]不是我想要的。

img1 img2

我希望ng-model="value"的值为UNIQUE。

这是demo

1 个答案:

答案 0 :(得分:1)

确保在输入中初始化数组(如果您希望它是一个简单的数组,否则默认情况下它将是一个对象):

$scope.inputs = {
    auction_name_account: [];
};

请参阅plunk:https://plnkr.co/edit/EkMZ6nJszuKt40K1FKOA?p=preview

(另请注意,因为你输入的最小长度,它不会出现在模型中,直到它传递10个字符,否则它将显示为空)

为了让您的错误跨度正常工作(回答以下评论中的问题),您需要修改输入中的名称以包含索引:

name="auction_name_account_{{value.selected}}"

然后在您的跨度中使用该名称(假设您的表单被称为&#34; sellItem&#34;):

<span class="error sell__input-text sell__input-text--big-input" data-ng-show="sellItem.auction_name_account_{{value.selected }}.$error.required">Wymagane!</span>