Search code examples
javascriptangularjsangularjs-scope

$scope with property says undefined


My controller

App.controller('Ctrl', ['$scope', function ($scope) {
     $scope.user = {};
    $scope.view = function () {
                console.log($scope.user);
                console.log($scope.user.length);
    }]);

My html

<input type="text" ng-model="user.lastname" />
<input type="email" ng-model="user.email" />
<input type="text" ng-model="user.address" />
<input type="email" ng-model="user.id" />
<input type="text" ng-model="user.city" />
<input type="email" ng-model="user.country" /> ---> This will be a select

No fields are mandatory, i have to do a search function based on user inputs. I have to validate only one thing that user have to provide atleast one data. When i put console as above, i can see value in first one, whereas the .length console returns undefined.

enter image description here


Solution

  • There is no length property for an object. Use Object.keys length instead

    Object.keys($scope.user).length;