我的 Controller
App.controller('Ctrl', ['$scope', function ($scope) {
$scope.user = {};
$scope.view = function () {
console.log($scope.user);
console.log($scope.user.length);
}]);
我的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
没有必填字段,我必须根据用户输入执行搜索功能。我只需验证用户必须提供至少一项数据的一件事。当我按照上面的方式放置控制台时,我可以看到第一个控制台中的值,而 .length 控制台返回未定义。
最佳答案
对象没有长度属性。 使用 Object.keys 长度代替
Object.keys($scope.user).length;
关于javascript - 带有属性的 $scope 表示未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654346/