2

私はAngularjsアプリケーションで作業しており、単純なフォームを持っています. jQueryシリアライズのような、すべてのフォームデータを取得する簡単な方法を作成する必要があります。しかし、データバインディングは良い解決策ではないと思います。私のフォームには、次のような「フィールド」があります。

<div class="multiselect-container" >
<label>Countries:</label>
<ul>
    <li ng-repeat="country in countries" >
        <input type="checkbox" id="country_{{country.id}}"  value="{{country.id}}" />
        <label for="country_{{country.id}}"  >{{ country.name }}</label>
    </li>
</ul>

各「フィールド」 I a は、一連の要素を選択できます。誰かが実装 () を解決する良い方法を教えてくれます。

ありがとう!

4

3 に答える 3

1

これを試すことができます http://docs.angularjs.org/api/ng/function/angular.toJson

このようにして、オブジェクトをjson文字列にシリアライズできます

于 2014-03-13T09:36:22.150 に答える
1

データバインディングは悪い解決策ではないと思います.とにかく、データをシリアル化し、指定したプロパティの値として設定するカスタムディレクティブを使用できます:

function MC($scope) {
    $scope.$watch('prop', function (v) {
        console.log(v);
    });
}

angular.module('ng')
.directive('formSerialization', function () {
    return {
        scope: {
            'formSerialization': '='
        },
        link: function (scope, elem, attrs) {
            console.log(attrs);
            $(elem).closest('form').submit(function () {
                var form = this;
                scope.$apply(function () {
                    scope.formSerialization = $(form).serialize();
                });
            });
        }
    };
});

次のマークアップで使用できます。

<form ng-app="" ng-controller="MC">
    <input name="txt" type="text" ng-model="prop" />
    <input data-form-serialization="prop" type="submit" value="Submit" />
</form>

そして、これが JSFiddle DEMOです。

于 2013-11-03T21:28:44.603 に答える
1

回答ありがとうございます。質問を適切に公開できず申し訳ありませんが、簡単な解決策を見つけました。

<div class="multiselect-container" >
    <label>Countries:</label>
    <ul>
        <li ng-repeat="country in countries" >
           <input type="checkbox" id="country_{{country.id}}" ng-model="data.countries[country.id]"  value="{{country.id}}" />
           <label for="country_{{country.id}}"  >{{ country.name }}</label>
        </li>
    </ul>
</div>

ありがとう!

于 2013-11-04T21:03:43.840 に答える