【发布时间】:2016-01-30 05:12:56
【问题描述】:
对 AngularJS 来说是全新的,在这里。我有一个页面,其中包含未定义数量的输入框,用户将在其中输入跟踪号。可能只有 1 个,也可能有 10 个。
我需要从每个输入框中获取数据。我目前陷入困境,因为我无法弄清楚如何从所有输入框中获取所有数据;我只能从第一个获取数据。
HTML
<input type="text" class="form-control" ng-model="inBox">
<!-- There are buttons and jQuery that will add another one of the
same exact input box above onto this page. There might be as many
as 15 of these input boxes on the page, and I need the values typed
into each one. -->
<input type="submit" ng-click="insert()">
AngularJS
var app = angular.module("myApp", []);
app.controller("mailroomController", function($scope, $http) {
$scope.insert = function() {
alert($scope.inBox);
};
});
我尝试过的:我尝试使用谷歌搜索,但没有成功。我尝试用$scope.inbox[1] 或$scope.inbox[2] 替换$scope.inBox。没有运气。
从输入框中获取每个数据的最佳方法是什么?
更新
为了获得我需要的解决方案,我将 jQuery 放在了我的 AngularJS 函数中。我的新 AngularJS 函数如下所示:
$scope.insert = function() {
$('.scan-packages').each(function() {
console.log(this.value);
});
};
从技术上讲,我已经解决了这个问题,但我是使用 jQuery 解决的。如果有人想将上述解决方案翻译成我将如何使用纯 AngularJS 获得该解决方案,请随时这样做,以便我可以使用该解决方案并感谢您回答问题。
【问题讨论】:
-
你能展示一下 jQuery 吗?看来您应该使用
ng-repeat -
您好,感谢您的回复。我已经使用 ng-repeat 来显示一些项目。稍后我会尝试发布它。重申一下,当我的用户单击提交按钮时,Angular 函数会提取第一个输入框中的内容,但我不知道也找不到提取其余部分的语法。
标签: javascript html angularjs input angular-ngmodel