Angularjs相当于连载 [英] Angularjs equivalent to serialize
问题描述
我在Angularjs应用程序的工作,我有一个简单的表格。我需要创建一个简单的方法来获取所有表单数据,像jQuery的序列化。但我一点数据绑定并不是一个很好的解决方案。我form'll有一些田是这样的:
I'm working in Angularjs application and I have a simple form. I need create a simple way to get all form data, something like jQuery serialize. But I thing data binding isn't a good solution. My form'll have some "fields" like this:
<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>
每个场我一个可以选择一组元素。
有人可以告诉我解决实现()的好方法。
Each "field" I a can select a set of elements. Someone can tell me a good way to solve implement ().
谢谢!
推荐答案
我认为数据绑定是个不错的解决办法,反正你可以使用自定义指令,它会序列数据,并设置为值属性您指定
I think data-binding is not a bad solution, anyway you can use custom directive which will serialize your data and set it as value to property you've specified:
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();
});
});
}
};
});
您可以用下面的加价使用它:
You can use it with the following mark-up:
<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 。
这篇关于Angularjs相当于连载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!