具有ng-model的名称数组 [英] Name array with ng-model

查看:82
本文介绍了具有ng-model的名称数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个添加按钮,该按钮使用指令将表的(.estimates)tbody添加到表中:

I have an add button that uses a directive to add-to a table's (.estimates) tbody:

function EstimateCtrl( $scope, $compile ) {

    $scope.services = [
            { 'value': 'c', 'name': 'Standard Courier' },
            { 'value': 'xc', 'name': 'Express Courier' },
            { 'value': 'cc', 'name': 'Country Courier' }
    ]

    $scope.add = function() {

        angular.element('.estimates tbody').append( $compile('<tr estimate></tr>')($scope) );

    }

}

angular.module('dashboard', [])
    .directive('estimate', function() {

        return {

            restrict: 'A',
            template: '<td><input type="text" placeholder="Suburb"/></td><td><select ng-model="estimate.service" ng-options="service.value as service.name for service in services" class="form-control"></select></td><td>$0.00</td><td><button type="button" class="remove">x</button></td>',
            link: function( scope, element, attrs ) {

                element.find('.remove').bind('click', function() {

                    element.closest('tr').remove();

                });

            }

        }

    });

如何在angularjs中使用ng-model获得元素数组?例如:

How can I have an element array using ng-model in angularjs? For example:

<select name="foo[]"></select>

<select ng-model="foo[]"></select>

我已经闲逛了一天半,但似乎无法休息一下.我希望也许有人能指出我正确的方向.非常感谢您的帮助.

I've been digging around for a day and half but I can't seem to catch a break. I was hoping that maybe someone can point me in the right direction. Thank you very much for any help.

这是指向unk子的链接,我确定看到每个人都会知道我在干什么: http://plnkr.co/edit/JlYB9P0vyAqghOmeNYh4

Here is the link to the plunker I'm sure after seeing this everyone is going know what I'm on about: http://plnkr.co/edit/JlYB9P0vyAqghOmeNYh4

Edit2:让我们看看是否可以再举一个例子来说明我的追求

Let's see if I can give you all another example to show you what I'm after

<form method="POST" action="">

<!-- I was attempting to do ng-model="estimate.service[]" but of course this doesn't work -->
<select name="estimate[service][]">
    <option value="foor">Foo</option>
    <option value="bar">Bar</option>
</select>
<select name="estimate[service][]">
    <option value="foor">Foo</option>
    <option value="bar">Bar</option>
</select>
<select name="estimate[service][]">
    <option value="foor">Foo</option>
    <option value="bar">Bar</option>
</select>

<input type="submit" value="Submit"/>

</form>

<?php

if ( $_POST )
{

    print_r( $_POST['estimate']['service'] );

}

?>

输出

推荐答案

好的!我设法找到了解决方法.

Ohrighty! I managed to find a work around.

我已经放弃了指令,并做了另一种方式,这是我的工作代码:

I have abandoned directives and did it another way, here is my working code:

HTML:

<div ng-controller="Ctrl">
    <table>
        <tbody ng-repeat="service in estimate.services">
            <tr>
                <td><input type="text" placeholder="Suburb"/></td>
                <td>
                    <select ng-model="estimate.services[service.name]" ng-options="option.value as option.name for option in options" class="form-control"></select>
                </td>
                <td>$0.00</td>
                <td><button type="button" class="remove">x</button></td>
            </tr>
        </tbody>
    </table>
</div>

JavaScript:

function Ctrl( $scope, $compile ) {

    $scope.estimate.services = [
        { name: 'service1', value: '' }
    ];

    $scope.options = [
        { name: 'Option 1', value: 'opt1' },
        { name: 'Option 2', value: 'opt2' },
        { name: 'Option 3', value: 'opt3' }
    ];

    $scope.add = function() {

        $scope.estimate.services.push({
            name: 'service' + ($scope.estimate.services.length + 1),
            value: ''
        });

    };

}

这篇关于具有ng-model的名称数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆