无法设置 属性 '$submitted' 的未定义错误 - Jasmine AngularJS
Cannot set property '$submitted' of undefined error - Jasmine AngularJS
我正在尝试测试我的控制器的一个功能,在该功能中我尝试设置 $submitted 属性 形式如下。
$scope.submitForm=function(){
$scope.form.$submitted = false;
$scope.done = true;
// code
};
当我从我的测试中调用这个函数时:
it('should be called', function () {
contactUsController.submitForm();
expect(contactUsController.submitForm).toHaveBeenCalled();
});
错误:
TypeError: Cannot set property '$submitted' of undefined
您需要先将 $scope.form
初始化为一个对象,然后才能对其设置属性。否则,您将在不存在的对象上设置 属性,因此会出现 can't set property of undefined
.
错误
只需像这样编辑您的代码:
$scope.submitForm=function(){
$scope.form = {};
$scope.form.$submitted = false;
$scope.done = true;
// code
};
由于我的评论有所帮助,请在控制器的开头添加以下代码:
$scope.form = {};
我正在尝试测试我的控制器的一个功能,在该功能中我尝试设置 $submitted 属性 形式如下。
$scope.submitForm=function(){
$scope.form.$submitted = false;
$scope.done = true;
// code
};
当我从我的测试中调用这个函数时:
it('should be called', function () {
contactUsController.submitForm();
expect(contactUsController.submitForm).toHaveBeenCalled();
});
错误:
TypeError: Cannot set property '$submitted' of undefined
您需要先将 $scope.form
初始化为一个对象,然后才能对其设置属性。否则,您将在不存在的对象上设置 属性,因此会出现 can't set property of undefined
.
只需像这样编辑您的代码:
$scope.submitForm=function(){
$scope.form = {};
$scope.form.$submitted = false;
$scope.done = true;
// code
};
由于我的评论有所帮助,请在控制器的开头添加以下代码:
$scope.form = {};