角度嵌套对象 - TypeError:无法读取未定义的属性“app”

时间:2022-06-13 19:43:01

I have nested objects like below:

我有如下嵌套对象:

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

But I'm getting below error:

但我得到以下错误:

TypeError: Cannot read property 'app' of undefined

Is there a way to get define this so errors do not occur?

有没有办法定义这个,所以不会发生错误?

I've tried doing things like $scope.userForm.app = {} but not sure if this is the right solution?

我已经尝试过像$ scope.userForm.app = {}这样的事情,但不确定这是否是正确的解决方案?

I also have below:

我也有以下内容:

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

and in controller

并在控制器中

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

This however does not bind with model correctly and does not output Scheduled in the form

但是,这不能正确绑定模型,也不会在表单中输出Scheduled

1 个解决方案

#1


2  

The error is saying userForm is undefined. Read the error message carefully carefully. cannot read property app of undefined. So you need to set userForm to an object:

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

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

#1


2  

The error is saying userForm is undefined. Read the error message carefully carefully. cannot read property app of undefined. So you need to set userForm to an object:

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

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