Allenph Allenph - 5 months ago 13
AngularJS Question

Why is an object utilized by Angular undefined?

I'm fairly new to Angular. Here is a controller I'm working on...

svs.controller('registrationCtrl', function($scope, validatorService) {
$scope.$watch("", function(newValue, oldValue) {
if (validatorService.validateEmail(newValue)) {
$ = true;
} else {
$ = false;

On the associated view, there is a text input for the user's email. It's set to have Angular use
as the model. This seems to be the case, as if I remove everything from inside the
function, and just do a simple console log, it logs whenever I change the value of the text input.

The idea here is to have an object at
that looks similar to this...

email: {
value: "",
valid: true

I'm attempting to watch the value of the text area, use a service method to validate the email, and setting the
property of
when it is valid.

Unfortunately, I'm getting an error...

TypeError: Cannot read property 'email' of undefined

I have not explicitly defined in the JavaScript
, nor have I made any reference to it in the HTML of my view.

Do I need to create this property before setting it? What is going on here?


yes you have to create a property before setting.