gpt4 book ai didi

javascript - AngularJS 数据模型架构

转载 作者:行者123 更新时间:2023-12-03 09:05:03 25 4
gpt4 key购买 nike

我的 Angular 应用程序上有以下内容:

$scope.things = [{
title: 'Simple',
type: 1,
form: {
input: [1, 2, 3],
clone: true
}
}];

$scope.clone = function(item) {
item.form.input.push(Math.floor(Math.random() * 999));
};

在 HTML 部分:

<div ng-repeat="item in things" class="item">
<h2>{{item.title}}</h2>
<div ng-repeat="input in item.form.input">
<input type="text" />
</div>
<button ng-click="cloneInput(item)">Clone</button>
</div>

当按下克隆按钮时,我将一个新元素推送到 form.input 数组并向 DOM 添加新输入。

我想 $http.post 输入的所有值。

我知道推送我需要使用的东西

$http.post('/path/to/my/api', {my object}).callback()

但我不知道如何从所有 .item 输入中创建一个对象。

有人可以向我解释一下方法或提出更好的解决方案吗?

最佳答案

如果您使用 ng-model 作为输入并将其设置为一个对象,然后您可以将该对象注入(inject)到帖子中,这是一个非常基本的示例:

JSFiddle

HTML:

<div ng-app="myApp" ng-controller="dummy">
<div ng-repeat="input in items">
<input type="text" name="{{input.name}}" ng-model="data[input.name]" />
</div>
<button ng-click="submit()">Submit</button>
<p ng-show="displayIt">{{data}}</p>
</div>

JS:

angular.module('myApp', [])
.controller('dummy', ['$scope', function ($scope) {
$scope.items = [{
name: 'test'
}, {
name: 'test2'
}];

$scope.data = {};
$scope.displayIt = false;
$scope.submit = function () {
// This is only to check it
$scope.displayIt = true;
// Do your post here
};

}]);

关于javascript - AngularJS 数据模型架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com