I am trying to test a function of my controller, Inside that function I trying to set the $submitted property of form like following.
$scope.submitForm=function(){
$scope.form.$submitted = false;
$scope.done = true;
// code
};
When I call this function from my test like:
it('should be called', function () {
contactUsController.submitForm();
expect(contactUsController.submitForm).toHaveBeenCalled();
});
Error:
TypeError: Cannot set property '$submitted' of undefined
答案 0 :(得分:1)
You need to initialize $scope.form
as an object before you can set properties on it. Otherwise you are setting a property on a non existant object and hence get an error of can't set property of undefined
.
Just edit your code like this:
$scope.submitForm=function(){
$scope.form = {};
$scope.form.$submitted = false;
$scope.done = true;
// code
};
答案 1 :(得分:0)
在我的评论帮助下,在控制器的开头添加以下代码:
$scope.form = {};