如何在js对象中添加更多学生记录?
如何添加更多的学生记录到角度数组,如:firstName,last Name,费用(在$ scope.student中)?如何在js对象" title="js对象">js对象中添加更多学生记录?
这是我有:
var mainApp = angular.module("mainApp", []); mainApp.controller('studentController', function($scope) {
$scope.student = {
firstName: "Muhammad",
lastName: "Waqas",
fees:500,
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
回答:
我不清楚了解这里的问题。但是你的目标是在$scope
中创建一个students
数组,你可以这样做。
mainApp.controller('studentController', function($scope) { $scope.students = [];
$scope.students.push({
firstName: "Muhammad",
lastName: "Waqas",
fees:500,
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
});
//add more students using .push()
});
在那里你有你的学生数组在$范围内。
回答:
你不能这样做,关于$scope.student
而是创建student
因为只有一个对象,并创建一个数组$scope.students
并存储在每个student
对象array.SImilarly您可以创建var student1,var student2
存储约其他学生
mainApp.controller('studentController', function ($scope) { $scope.students = [];
$scope.studentFullName = function() {
};
var student = {
firstName: "Muhammad",
lastName: "Waqas",
fees: 500,
subjects: [
{name: 'Physics', marks: 70},
{name: 'Chemistry', marks: 80},
{name: 'Math', marks: 65}
],
fullName: this.firstName + ' ' + this.lastName
};
$scope.students.push(student);
});
以上是 如何在js对象中添加更多学生记录? 的全部内容, 来源链接: utcz.com/qa/259792.html