对象的属性显示为undefined

时间:2017-07-20 12:01:15

标签: angularjs

我创建了edit_stat对象,通过resolve属性将其传递给另一个控制器。但是对象的属性值显示为undefined.i也显示了日志输出。无法解决为什么它显示为这样的问题。

angular.module('fitgalaxyApp').controller('userProfileCtrl', function($scope, apiService, $uibModal) {


var url = "coaching/getUserstatsGoal/0";
  apiService.getDataWithToken(url, {}, 'GET').then(function(success) 
  {
    $scope.current_stat = [];
    $scope.goal =[];
    $scope.activity="";
    var edit_stat={};
      if (success.status == 200) 
      {
          var current_stat = success.data.currentStats;
          $scope.first_name = current_stat.first_name;
          $scope.last_name = current_stat.last_name;
          $scope.weight = current_stat.weight;
          $scope.height = current_stat.height;
          $scope.bodyfat = current_stat.bodyfat;
          $scope.age = current_stat.age;
          $scope.activity_level_id = current_stat.activity_level_id;
          $scope.birthdate = current_stat.birthdate;
          $scope.gender = current_stat.gender; 
          $scope.avatar = current_stat.avatar;

          //Goals Data
          var goal = success.data.goals;
          $scope.weigh = goal.weight.quantitative.weight;

              if(goal.weight.qualitative_entry){
                        $scope.weight_comment = [];
                        for (var i = 0; i < goal.weight.qualitative.length; i++) {
                          $scope.weight_comment.push(goal.weight.qualitative[i].comment);
                      }
                }

          //nutrition data
          $scope.calories = goal.nutrition.quantitative_day.calories;
          $scope.protein = goal.nutrition.quantitative_day.protein;
          $scope.carbohydrate = goal.nutrition.quantitative_day.carbohydrate;
          $scope.fat = goal.nutrition.quantitative_day.fat;
          $scope.water_consumed = goal.nutrition.quantitative_day.water_consumed;

          if(goal.nutrition.qualitative_entry){
                $scope.nutrition_comment = [];
                for (var i = 0; i < goal.nutrition.qualitative.length; i++) {

                  $scope.nutrition_comment.push(goal.nutrition.qualitative[i].comment);
              }
            }

          //exercise data
          $scope.calorie = goal.exercise.quantitative_week.calories;
          $scope.aerobic_minutes = goal.exercise.quantitative_week.aerobic_minutes;
          $scope.no_of_strength_exercises = goal.exercise.quantitative_week.no_of_strength_exercises;

          if(goal.exercise.qualitative_entry){
                $scope.exercise_comment = [];
                for (var i = 0; i < goal.exercise.qualitative.length; i++) {
                  $scope.exercise_comment.push(goal.exercise.qualitative[i].comment);
              }
            }  

          //sleep data
          $scope.duration = goal.sleep.quantitative_day.day_duration;
          $scope.bedtime = goal.sleep.quantitative_day.day_bed_time;  
          if(goal.sleep.qualitative_entry){
                $scope.sleep_comment = [];
                for (var i = 0; i < goal.sleep.qualitative.length; i++) {

                  $scope.sleep_comment.push(goal.sleep.qualitative[i].comment);
              }
            }

          //sunlight data
          $scope.total = goal.sunlight.quantitative_day.total_sunlight;
          $scope.direct = goal.sunlight.quantitative_day.direct;
          $scope.indirect = goal.sunlight.quantitative_day.indirect;
          if(goal.sunlight.qualitative_entry){
                $scope.sunlight_comment = [];
                for (var i = 0; i < goal.sunlight.qualitative.length; i++) {
                  $scope.sunlight_comment.push(goal.sunlight.qualitative[i].comment);
              }
            }


          if(parseInt($scope.activity_level_id)==1){
            $scope.activity = 'Sedentary';
            return $scope.activity;
          }
          else if(parseInt($scope.activity_level_id)==2){
            $scope.activity = 'Lightly Active';
            return $scope.activity;
          }
          else if(parseInt($scope.activity_level_id)==3){
            $scope.activity = 'Moderately Active';
            return $scope.activity;
          }
          else if(parseInt($scope.activity_level_id)==4){
            $scope.activity = 'Very Active';
            return $scope.activity;
          }
          else if(parseInt($scope.activity_level_id)==5){
            $scope.activity = 'Extremely Active';
            return $scope.activity;
          }  
      }

});
 var edit_stat = {
  'Weight': $scope.weight,
  'Bodyfat': $scope.bodyfat,
  'Date of Birth': $scope.birthdate,
  'Height': $scope.height,
  'Gender': $scope.gender,
  'Activity Level': $scope.activity,
}
console.log(JSON.stringify(edit_stat));
 console.log(edit_stat);
/*edit_stat.weight = $scope.weight;
edit_stat.bodyfat = $scope.bodyfat;
edit_stat.birthdate = $scope.birthdate;
edit_stat.height = $scope.height;
edit_stat.gender = $scope.gender;
edit_stat.activity = $scope.activity;

console.log(typeof edit_stat);*/

$scope.editWeight = function (){
    $uibModal.open({
        templateUrl: 'templates/editWeightPopup.html',
        controller: 'editWeightPopupCtrl',
          resolve: {
          weight: function() {
            return $scope.weigh;
          }
        } 
      });
  }
$scope.$on('weight', function(event, args) {
  $scope.weigh = args;
});

$scope.editStats = function(){
  $uibModal.open({
    templateUrl: 'templates/editStatsPopup.html',
    controller: 'editStatsPopupCtrl',
       resolve: {
        edit_stat: function(){
          return $scope.edit_stat;
        }
       }

  })
}

});

Object {Weight: undefined, Bodyfat: undefined, Date of Birth: undefined, Height: undefined, Gender: undefined…}Activity Level: undefinedBodyfat: undefinedDate of Birth: undefinedGender: undefinedHeight: undefinedWeight: undefined__proto__: Object

1 个答案:

答案 0 :(得分:0)

记录success.data并查看从服务器发回的内容。看看套管,因为这可能会导致你的问题。