如何在js对象中添加更多学生记录?

时间:2017-12-03 06:47:18

标签: javascript angularjs

如何将更多学生记录添加到角度数组中,例如:firstName,last Name,费用($ scope.student)?

这就是我所拥有的:

 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;
               }
            };
         });

2 个答案:

答案 0 :(得分:0)

我在这里不清楚这个问题。但你打算做的是在students中创建一个$scope数组,你可以这样做。

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中。

答案 1 :(得分:0)

您不能在$scope.student上执行此操作,而只是创建student作为对象并创建数组$scope.students并将每个student对象存储在该数组中。您可以创建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);
    });