Javascript 使用Angularjs在作用域中循环
通过ng repeat进行循环很容易,因为您有json对象,并且通过键可以根据需要循环和收集数据。 我这里的问题是如何循环特定的作用域或输入,以获得包含在ng repeat中但不包含在json对象中的值 以下是完整代码: 解释 我有一个非常简单的对象,没有像这样的键Javascript 使用Angularjs在作用域中循环,javascript,angularjs,json,Javascript,Angularjs,Json,通过ng repeat进行循环很容易,因为您有json对象,并且通过键可以根据需要循环和收集数据。 我这里的问题是如何循环特定的作用域或输入,以获得包含在ng repeat中但不包含在json对象中的值 以下是完整代码: 解释 我有一个非常简单的对象,没有像这样的键 var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"]; 然后,我在这个简单结构的表中列出了这些值,如下所示 <div data-ng-app="m
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>
{{oneFile}}
var arr=[“~\\191746.JPG”、“~\\191848.JPG”、“~\\191908.JPG”];
var-app=angular.module(“myApp”,[]);
app.controller(“myCtrl”,函数($scope){
$scope.ShowFiles=arr;
$scope.save=函数(){
var msg=document.getElementById(“msg”);
var指数=0;
$scope.ShowFiles.forEach(函数(一个文件,命名){
msg.innerHTML=
msg.innerHTML+'row#'+(index++)+':'+JSON.stringify(oneFile)+'--'+naming++'
;
});
};
)};
当我按下#保存按钮时,收集两列的所有数据是否(包括在范围“ShowFiles”中,例如{{oneFile}})
或
(不包括在范围“ShowFiles”中,如[input data ng model=“naming”])-“此处有问题” 显示如下所示 一个文件---命名
第0行:“~\191746.JPG”--“动物”
第1行:“~\191848.JPG”--“汽车”
第2行:“~\191908.JPG”--“朋友”
但不幸的是,命名显示的序列号如下所示:0,1,2要实现此功能,您需要用以下内容替换输入元素标记:
<input data-ng-model="fileNames[$index]" type="text" style="width: 200px" />
您的控制器代码如下所示:
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 />';
});
};
});
var arr=[“~\\191746.JPG”、“~\\191848.JPG”、“~\\191908.JPG”];
var-app=angular.module(“myApp”,[]);
app.controller(“myCtrl”,函数($scope){
$scope.ShowFiles=arr;
$scope.fileNames=[];
$scope.save=函数(){
var msg=document.getElementById(“msg”);
$scope.ShowFiles.forEach(函数(一个文件,索引){
msg.innerHTML=
msg.innerHTML+'row#'+(index+1)+':'+JSON.stringify(oneFile)+'--'+$scope.fileNames[index]+'
';
});
};
});
要实现此功能,您需要将输入元素标记替换为以下内容:
<input data-ng-model="fileNames[$index]" type="text" style="width: 200px" />
您的控制器代码如下所示:
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 />';
});
};
});
var arr=[“~\\191746.JPG”、“~\\191848.JPG”、“~\\191908.JPG”];
var-app=angular.module(“myApp”,[]);
app.controller(“myCtrl”,函数($scope){
$scope.ShowFiles=arr;
$scope.fileNames=[];
$scope.save=函数(){
var msg=document.getElementById(“msg”);
$scope.ShowFiles.forEach(函数(一个文件,索引){
msg.innerHTML=
msg.innerHTML+'row#'+(index+1)+':'+JSON.stringify(oneFile)+'--'+$scope.fileNames[index]+'
';
});
};
});