<!DOCTYPE html>
<html ng-app="my_app">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("my_app", []);
app.controller('my_controller', function($scope) {
$scope.students = [{
studentno: 's001',
studentname: '学生1',
},
{
studentno: 's002',
studentname: '学生2',
},
{
studentno: 's003',
studentname: '学生3',
},
{
studentno: 's004',
studentname: '学生4',
},
{
studentno: 's005',
studentname: '学生5',
},
];
// angularjs循环获取对象,无法跳出循环,通过标记字段跳出判断
$scope.getStudent = function(studentno) {
var student = null;
var keepGoing = true;
angular.forEach($scope.students, function(item, index, array) {
console.log(item.studentname);
if(keepGoing) {
if(studentno == item.studentno) {
student = item;
keepGoing = false;
}
}
});
return student;
}
$scope.init = function() {
var student = $scope.getStudent('s003');
console.log(JSON.stringify(student));
}
$scope.init();
});
</script>
</head>
<body ng-controller="my_controller">
</body>
</html>
06-08
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)