title: scope array in angularjs is undefined
body: <p>I am building an angularjs app but I have an issue with $scope var.</p>
  
  <p>I have the following definition:</p>
  
  <pre><code>$scope.data = {
          object: {
            id: undefined,
            name: undefined,
            category:
            {
              id: undefined,
              name: undefined
            },
            description: undefined,
            featured: 1,
            seassons: undefined
          }, 
          progress: 0,
          emptyResponse : false
      };
  </code></pre>
  
  <p>The issue is that when I tried to set the property $scope.data.object.name I get the following error in console: TypeError: Cannot set property 'name' of undefined.
  Is there any way to initialize the arrays inside $scope var? I tried </p>
  
  <pre><code>$scope.data.object = [] 
  </code></pre>
  
  <p>which works, but just in the scope of that function, and also if I call that function again I will lose the previous data.
  Is there any way to do this? Another way to do it works, is if I set the value of that property from the view through ng-model, but in some cases I need to do some logic in my controller.</p>
  
  <p>The controller is correctly defined</p>
  
  <pre><code>function VideoCreateController($scope,....)
  </code></pre>
  
  <p>Full controller, the method that is failing is IsValid, but also if I execute Create function without completing some fields in the view (when I tried to assign those values I get undefined)</p>
  
  <pre><code>(function() {
      'use strict';
  
      angular
          .module('naut')
          .controller('VideoCreateController', VideoCreateController);
  
      function VideoCreateController($scope, $rootScope, $location, $state, SessionService, ProgramFactory,  SeassonFactory, VideoFactory, CategoryFactory, SubcategoryFactory, $filter, ngTableParams, SweetAlert, $routeParams, $translate){
          var vm = this;
  
      $rootScope.baseurl = "#/app/Video/";
      $scope.data = {
          object: {
            id: undefined,
            name: undefined,
            category:
            {
              id: undefined,
              name: undefined
            },
            description: undefined,
            featured: 1,
            belongtoprogram: false,
            isfeatured: false,
            adv: false,
            images: undefined,
            preview: undefined,
            categories: undefined,
            subcategories: undefined,
            programs: undefined,
            episode: undefined,
            seassons: undefined
          }, 
          progress: 0,
          videosubmitted: false,
          uploading: false,
          errorMessage: undefined,
          error: false,
          emptyResponse : false
      };
  
      $scope.image = null;
      $scope.imageFileName = '';
  
      $scope.uploadme = {};
      $scope.uploadme.src = '';
  
      var obj = [];
      $scope.data.object = obj;
      $scope.GetInformation = function() {
      $rootScope.$emit('LOAD');
       CategoryFactory.GetAll()
            .then(function(response){
                ProgramFactory.GetAll()
                  .then(function(programresponse){
                if (response.success &amp;&amp; programresponse.success) {
                    $scope.data.object.categories = response.data;
                    $scope.data.object.subcategories = null;
                    $scope.data.object.programs = programresponse.data;
                    $scope.data.object.seassons = null;
                    console.log($scope.data.object);
                }
                else if(response.code == "ERROR__NOT_AUTHORIZED")
                {
                  $location.path('/NotAuthorized');
                }
                else
                {
                    $scope.data.error = true;
                    $scope.data.errorMessage = response.code;
                }
                $rootScope.$emit('UNLOAD');
            }, function(response){
                  if (response == null) {
                      $scope.data.error = true;
                      $scope.data.errorMessage = "ERROR__SERVER_NON_WORKING";
                  }
                  else
                  {
                      $scope.data.error = true;
                      $scope.data.errorMessage = response.Message;    
                  }
                  $rootScope.$emit('UNLOAD');
              });
              });
      }
  
      $scope.GetSubcategories = function(id) {
      $rootScope.$emit('LOAD');
       SubcategoryFactory.GetAll(id)
            .then(function(response){
                console.log(response);
                if (response.success) {
  
                    $scope.data.object.subcategories = response.data;
                    console.log($scope.data.object);
                }
                else if(response.code == "ERROR__NOT_AUTHORIZED")
                {
                  $location.path('/NotAuthorized');
                }
                else
                {
                    $scope.data.error = true;
                    $scope.data.errorMessage = response.code;
                }
                $rootScope.$emit('UNLOAD');
            }, function(response){
                  if (response == null) {
                      $scope.data.error = true;
                      $scope.data.errorMessage = "ERROR__SERVER_NON_WORKING";
                  }
                  else
                  {
                      $scope.data.error = true;
                      $scope.data.errorMessage = response.Message;    
                  }
                  $rootScope.$emit('UNLOAD');
              });
      }
  
      $scope.GetSeassons = function(id) {
      $rootScope.$emit('LOAD');
       SeassonFactory.GetAll(id)
            .then(function(response){
                console.log(response);
                if (response.success) {
  
                    $scope.data.object.seassons = response.data;
                }
                else if(response.code == "ERROR__NOT_AUTHORIZED")
                {
                  $location.path('/NotAuthorized');
                }
                else
                {
                    $scope.data.error = true;
                    $scope.data.errorMessage = response.code;
                }
                $rootScope.$emit('UNLOAD');
            }, function(response){
                  if (response == null) {
                      $scope.data.error = true;
                      $scope.data.errorMessage = "ERROR__SERVER_NON_WORKING";
                  }
                  else
                  {
                      $scope.data.error = true;
                      $scope.data.errorMessage = response.Message;    
                  }
                  $rootScope.$emit('UNLOAD');
              });
      }
  
      $scope.IsValid = function() {
        var valid = true;
        if(!$scope.data.object.name)
          valid = false;
        if(!$scope.data.object.description)
          valid = false;
        if(!$scope.data.object.subcategory)
          valid = false;
        if($scope.data.object.belongtoprogram &amp;&amp; !$scope.data.object.episode &amp;&amp; !$scope.data.object.seasson)
          valid = false;
        return valid;
      }
  
      $scope.Create = function(isValid) {
        if (isValid) {
          $rootScope.$emit('LOAD');
          var params = {
            id: $scope.data.object.id,
            name: $scope.data.object.name,
            preview: $scope.data.object.preview,
            adv: $scope.data.object.adv,
            featured: $scope.data.object.featured,
            description: $scope.data.object.description,
            episode: $scope.data.object.episode,
            subcategory: 
            {
              id: $scope.data.object.subcategory.id
            },
            seasson: 
            {
              id: $scope.data.object.seasson !== undefined ? $scope.data.object.seasson.id : null
            }
          };
  
          if(!$scope.data.object.isfeatured)
            params.featured = 0;
  
          VideoFactory.Create(params).then(function(response){
            if (response.success) {
              $state.go('app.video_index');
            }
            else
            {
              $scope.data.error =  true;
              $scope.data.errorMessage = response.code;
            }
            $rootScope.$emit('UNLOAD');
          }, function(response){
            if (response == null) {
              $scope.data.error = true;
              $scope.data.errorMessage = "ERROR__SERVER_NON_WORKING";
            }
            else
            {
              $scope.data.error = true;
              $scope.data.errorMessage = response.code;    
            }
            $rootScope.$emit('UNLOAD');
          });
        };
  
        }
  
        $scope.selected = function(image)
        {
          $scope.data.object.preview = image.source;
        }
  
        var callback = function(e){
          $scope.$apply(function(){
            $scope.data.progress = Math.round(e.loaded / e.total * 100);  
          });
        };
        $scope.reset = function()
        {
  
          $scope.data.object.id = undefined;
          $scope.data.object.images = undefined;
          $scope.data.object.source = undefined;
          $scope.data.object.preview = undefined;
          $scope.data.object.description = undefined;
          $scope.data.videosubmitted = false;
          $scope.data.object.name = undefined;
          $scope.data.uploading = false;
  
        };
  
        $scope.upload = function(item) {
          $scope.data.videosubmitted = true;
          $scope.data.object.name = item.name.substr(0, item.name.lastIndexOf('.'));
          $scope.data.uploading = true;
          var params = {
            files: item
          };
          VideoFactory.Save(params, callback).then(function(response){
            response = JSON.parse(response);
            $scope.data.uploading = false;
            if (response.success) {
              $scope.data.object.id = response.data.id;
              $scope.data.object.images = response.data.images;
              $scope.data.object.source = response.data.source;
              $scope.data.object.preview = response.data.preview;
            }
            else
            {
              $scope.data.error =  true;
              $scope.data.errorMessage = response.code;
            }
  
          }, function(response){
            if (response == null) {
              $scope.data.error = true;
              $scope.data.errorMessage = "ERROR__SERVER_NON_WORKING";
            }
            else
            {
              $scope.data.error = true;
              $scope.data.errorMessage = response.code;    
            }
            $scope.data.uploading = false;
          });
        }
  
        $scope.GetInformation();
    }
  })();
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div ng-controller="VideoCreateController as controller" nv-file-drop="" uploader="uploader" filters="queueLimit, customFilter"&gt;
      &lt;a ng-href="{{baseurl}}Index" class="mr btn btn-labeled btn-default"&gt;
                 &lt;span class="btn-label"&gt;&lt;i class="fa fa-arrow-left"&gt;&lt;/i&gt;
                 &lt;/span&gt;&lt;span translate="PAGE.BACK" class="nopadding"&gt;&lt;/span&gt;&lt;/a&gt;
     &lt;div class="app-view-header"&gt;
     &lt;span translate="PAGE.CREATE" class="nopadding"&gt;&lt;/span&gt;&lt;small&gt;&lt;span translate="MODULES.VIDEO.A-VIDEO" class="nopadding"&gt;&lt;/span&gt;&lt;/small&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-12"&gt;
        &lt;div class="panel panel-default"&gt;
          &lt;div class="panel-heading"&gt;&lt;h1&gt;&lt;span translate="MODULES.VIDEO.CREATE" class="nopadding"&gt;&lt;/span&gt;&lt;/h1&gt;&lt;/div&gt;
            &lt;div class="panel-body"&gt;
              &lt;div class="row"&gt;
                &lt;div class="col-md-12"&gt;
                  &lt;form role="form" name="data" class="mb-lg" ng-submit="Create(data.$valid)" novalidate&gt;
                      &lt;div class="row" ng-show="data.error"&gt;
                        &lt;div class="col-md-12"&gt;
                          &lt;div class="alert ng-isolate-scope alert-danger alert-dismissable" ng-class="['alert-' + (type || 'warning'), closeable ? 'alert-dismissable' : null]" role="alert" type="danger"&gt;
                          &lt;div&gt;
                            &lt;span class="ng-binding ng-scope"&gt;&lt;span translate="ERROR.{{data.errorMessage}}" class="nopadding"&gt;&lt;/span&gt;&lt;/span&gt;
                          &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                 &lt;/div&gt;
                 &lt;div class="row"&gt;
                  &lt;div class="col-md-12"&gt;
                      &lt;div class="row" ng-show="!data.videosubmitted"&gt;
                          &lt;div class="col-md-10 no-padding-right"&gt;
                              &lt;div class="dropzone" id="dropbox" callback-fn="upload(video)" file-dropzone="[video/mp4, video/3gpp,   video/quicktime, video/x-msvideo, video/x-ms-wmv]"
                              file="image" file-name="imageFileName" data-max-file-size="3000"&gt;
                                  &lt;span translate="MODULES.VIDEO.DROPVIDEO"&gt;&lt;/span&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-md-2 upload-btn no-padding-left"&gt;
                              &lt;label class="upload-search btn btn-primary  no-padding"&gt;
                              &lt;div&gt;
                                  &lt;input type="file" onchange="angular.element(this).scope().upload(this.files[0])"/&gt;
                                  &lt;i class="fa fa-upload"&gt;&lt;/i&gt;
                                  &lt;span translate="COMMON.FILESEARCH"&gt;&lt;/span&gt;
                              &lt;/div&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="video-data" ng-show="data.videosubmitted"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-8"&gt;
                          &lt;div class="row margin-bottom-small"&gt;
                            &lt;div class="col-md-6"&gt;
                                  &lt;div class="form-group"&gt;
                                      &lt;label for="name" class="col-sm-2 control-label" translate="COMMON.NAME"&gt;&lt;/label&gt;
                                      &lt;div class="col-sm-10"&gt;
                                          &lt;input id="name" ng-model="data.object.name" type="text" required ng-class="{ 'has-error' : data.object.name.$invalid &amp;&amp; !data.object.name.$pristine }" ng-minlength="2" class="form-control"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                  &lt;div class="row margin-bottom-small"&gt;
                        &lt;div class="col-md-6"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="col-md-12"&gt;
                                      &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.CATEGORY.CATEGORY"&gt;&lt;/label&gt;
                                      &lt;div class="col-sm-10"&gt;
                                          &lt;ui-select ng-model="data.object.category" ng-change="GetSubcategories(data.object.category.id)" class="text-left"&gt;
                                             &lt;ui-select-match placeholder="Select a category..."&gt;{{data.object.category.name}}&lt;/ui-select-match&gt;
                                             &lt;ui-select-choices repeat="item in data.object.categories"&gt;
                                                &lt;span ng-bind-html="item.name"&gt;&lt;/span&gt;
                                             &lt;/ui-select-choices&gt;
                                          &lt;/ui-select&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-6"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.SUBCATEGORY.SUBCATEGORY"&gt;&lt;/label&gt;
                              &lt;div class="col-sm-10"&gt;
                                  &lt;ui-select ng-model="data.object.subcategory" class="text-left"&gt;
                                     &lt;ui-select-match placeholder="Select a category..."&gt;{{data.object.subcategory.name}}&lt;/ui-select-match&gt;
                                     &lt;ui-select-choices repeat="item in data.object.subcategories"&gt;
                                        &lt;span ng-bind-html="item.name"&gt;&lt;/span&gt;
                                     &lt;/ui-select-choices&gt;
                                  &lt;/ui-select&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-12"&gt;
                          &lt;div class="checkbox c-checkbox pull-left mt0"&gt;
                              &lt;label class="col-md-12"&gt;
                                  &lt;input type="checkbox" ng-model="data.object.belongtoprogram" id="belongtoprogram" value="" /&gt;
                                  &lt;span class="fa fa-check checkbox-margin"&gt;&lt;/span&gt;&lt;label for="belongtoprogram" class="nopadding" translate="MODULES.VIDEO.INPROGRAM"&gt;&lt;/label&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row margin-bottom-small" ng-show="data.object.belongtoprogram"&gt;
                        &lt;div class="col-md-4"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="col-md-12"&gt;
                                      &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.PROGRAM.PROGRAM"&gt;&lt;/label&gt;
                                      &lt;div class="col-sm-10"&gt;
                                          &lt;ui-select ng-model="data.object.program" ng-change="GetSeassons(data.object.program.id)" class="text-left"&gt;
                                             &lt;ui-select-match placeholder="Select a program..."&gt;{{data.object.program.name}}&lt;/ui-select-match&gt;
                                             &lt;ui-select-choices repeat="item in data.object.programs"&gt;
                                                &lt;span ng-bind-html="item.name"&gt;&lt;/span&gt;
                                             &lt;/ui-select-choices&gt;
                                          &lt;/ui-select&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.SEASSON.SEASSON"&gt;&lt;/label&gt;
                              &lt;div class="col-sm-10"&gt;
                                  &lt;ui-select ng-model="data.object.seasson" class="text-left"&gt;
                                     &lt;ui-select-match placeholder="Select a seasson..."&gt;{{data.object.seasson.name}}&lt;/ui-select-match&gt;
                                     &lt;ui-select-choices repeat="item in data.object.seassons"&gt;
                                        &lt;span ng-bind-html="item.name"&gt;&lt;/span&gt;
                                     &lt;/ui-select-choices&gt;
                                  &lt;/ui-select&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.VIDEO.EPISODE"&gt;&lt;/label&gt;
                              &lt;div class="col-sm-4"&gt;
                                  &lt;input ng-model="data.object.episode" type="number" ng-class="{ 'has-error' : data.object.episode.$invalid &amp;&amp; !data.object.episode.$pristine }" ng-minlength="1" ng-maxlength="3" class="form-control"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row margin-bottom-small"&gt;
                        &lt;div class="col-md-12"&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;label for="name" class="col-sm-12 control-label" translate="MODULES.VIDEO.DESCRIPTION"&gt;&lt;/label&gt;
                                  &lt;div class="col-sm-12"&gt;
                                      &lt;div text-angular="" ng-model="data.object.description" name="data.object.description" class="btn-group-small"&gt;&lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-12"&gt;
                          &lt;div class="checkbox c-checkbox pull-left mt0"&gt;
                              &lt;label class="col-md-12"&gt;
                                  &lt;input type="checkbox" ng-model="data.object.adv" id="adv" value="" /&gt;
                                  &lt;span class="fa fa-check checkbox-margin"&gt;&lt;/span&gt;&lt;label for="adv" class="nopadding" translate="MODULES.VIDEO.ADVERTISEMENT"&gt;&lt;/label&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-12"&gt;
                          &lt;div class="checkbox c-checkbox pull-left mt0"&gt;
                              &lt;label class="col-md-12"&gt;
                                  &lt;input type="checkbox" ng-model="data.object.isfeatured" id="isfeatured" value="" /&gt;
                                  &lt;span class="fa fa-check checkbox-margin"&gt;&lt;/span&gt;&lt;label for="isfeatured" class="nopadding" translate="MODULES.VIDEO.FEATURED"&gt;&lt;/label&gt;
                              &lt;/label&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="row margin-bottom-small" ng-show="data.object.isfeatured"&gt;
                            &lt;div class="col-md-6"&gt;
                                  &lt;div class="form-group"&gt;
                                      &lt;label for="name" class="col-sm-2 control-label" translate="MODULES.VIDEO.ORDER"&gt;&lt;/label&gt;
                                      &lt;div class="col-sm-4"&gt;
                                          &lt;input ng-model="data.object.featured" type="number" ng-class="{ 'has-error' : data.object.featured.$invalid &amp;&amp; !data.object.featured.$pristine }" ng-minlength="1" ng-maxlength="2" class="form-control"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                  &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          &lt;div class="row"&gt;
                              &lt;div class="col-md-12"&gt;
                                  &lt;div ng-show="data.uploading"&gt;
                                      &lt;progressbar value="data.progress" class="progress-striped active"&gt;{{data.progress}}%&lt;/progressbar&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="row" ng-show="data.object.preview"&gt;
                              &lt;div class="col-md-12"&gt;
                                  &lt;img class="img-responsive center" src="/app/videos/{{data.object.source}}/screenshots/{{data.object.preview}}"&gt;
                              &lt;/div&gt;
                              &lt;div class="col-md-12"&gt;
                                  &lt;div ng-show="data.object.images != null"&gt;
                                      &lt;h5 class="page-header"&gt;&lt;span translate="MODULES.VIDEO.SELECT-PREVIEW"&gt;&lt;/span&gt;&lt;/h5&gt;
                                      &lt;div ng-repeat="image in data.object.images" class="col-lg-3"&gt;
                                          &lt;img class="img-thumbnail" ng-class="{ 'preview-selected': image.source == data.object.preview, 'preview-not-selected': image.source != data.object.preview }" ng-click="selected(image)" src="/app/videos/{{data.object.source}}/screenshots/{{image.source}}"&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;hr /&gt;
                  &lt;div class="row"&gt;
                &lt;div class="col-md-12 text-right"&gt;
                  &lt;button type="reset" class="btn btn-labeled btn-danger"  ng-click="reset()"&gt;
                      &lt;span class="btn-label"&gt;&lt;i class="fa fa-times"&gt;&lt;/i&gt;
                      &lt;/span&gt;&lt;span translate="PAGE.RESET" class="nopadding"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;button ng-disabled="data.$invalid &amp;&amp; !IsValid()" type="submit" class="btn btn-labeled btn-success"&gt;
                      &lt;span class="btn-label"&gt;&lt;i class="fa fa-check"&gt;&lt;/i&gt;
                      &lt;/span&gt;&lt;span translate="PAGE.CREATE" class="nopadding"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
                 &lt;/div&gt;
  
              &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>