Angular nested objects - TypeError: Cannot read property 'app' of undefined

Angular nested objects - TypeError: Cannot read property 'app' of undefined

我有如下嵌套对象:

$scope.userForm.app.status = 'Scheduled';
$scope.userForm.app.recurring = 'One-off';

但我遇到以下错误:

TypeError: Cannot read property 'app' of undefined

有没有办法定义它以免发生错误?

我尝试过 $scope.userForm.app = {} 但不确定这是否是正确的解决方案?

我还有以下:

input.form-control#status(ng-model='userForm.status', name='status', type='text', disabled='')

并在控制器中

$scope.userForm = {};
$scope.userForm.status = 'Scheduled';

然而,这并没有正确绑定模型,也没有以

的形式输出 Scheduled

错误是说 userForm 未定义。仔细阅读错误消息。无法读取未定义的 属性 app。所以你需要将userForm设置为一个对象:

$scope.userForm = {
  app: {
    status: 'Scheduled',
    recurring: 'One-off'
  }
};