通过 ng-repeat 循环很容易,因为你有 json 对象,并且通过键你可以根据需要循环和收集数据。 我的问题是如何循环特定范围或输入以获取包含在 ng-repeat 中但不包含在 json 对象中的值?
这里是完整代码:https://jsfiddle.net/medoo/wgc1my7d/
说明
我有一个非常简单的对象,没有像这样的键
var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"];
然后,我在结构简单的表格中列出了这些值,如下所示
<div data-ng-app="myApp" data-ng-controller="myCtrl">
<table id="fils">
<tr data-ng-repeat="oneFile in ShowFiles">
<td style="border: 1px solid #000000">{{oneFile}}</td>
<td style="border: 1px solid #000000">
<input data-ng-model="naming" type="text" style="width: 200px" />
<%--here is my problem !!!
i need to get values of element "input" or scope "naming" which not included with ShowFiles --%>
</td>
</tr>
</table>
<input id="save" data-ng-click="save()" type="button" value="Save" />
<div id="msg"></div>
</div>
<script>
var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"];
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
$scope.ShowFiles = arr;
$scope.save = function () {
var msg = document.getElementById("msg");
var index = 0;
$scope.ShowFiles.forEach(function (oneFile, naming) {
msg.innerHTML =
msg.innerHTML + 'row #' + (index++) + ': ' + JSON.stringify(oneFile) + ' --- ' + naming + '<br />';
});
};
)};
</script>
我需要的就是,当我按下#save按钮时,收集两列的所有数据是否
(包含在“ShowFiles”范围内,例如 {{oneFile}} )
或
(未包含在“ShowFiles”范围内,例如 [input data-ng-model="naming"] )--“问题在这里”
显示如下
oneFile --- 命名
第 0 行:“~\191746.JPG”---“动物”
第 1 行:“~\191848.JPG”---“汽车”
第 2 行:“~\191908.JPG”---“ friend ”
但不幸的是,命名会显示这样的序列号:0,1,2
最佳答案
要实现此功能,您需要将输入元素标记替换为:
<input data-ng-model="fileNames[$index]" type="text" style="width: 200px" />
以及您的 Controller 代码:
var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"];
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
$scope.ShowFiles = arr;
$scope.fileNames = [];
$scope.save = function () {
var msg = document.getElementById("msg");
$scope.ShowFiles.forEach(function (oneFile, index) {
msg.innerHTML =
msg.innerHTML + 'row #' + (index + 1) + ': ' + JSON.stringify(oneFile) + ' --- ' + $scope.fileNames[index] + '<br />';
});
};
});
关于javascript - 使用 Angularjs 循环范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44216127/