1

I am receiving data into my my $scope as an object that contains various details of a client. I have the following models

client.name
client.email
client.programme

In my function I wish to ensure that every key has a value and nothing is left undefined.

I thought I could use || so go through each one and check for undefined within an if statement however this does not work.

for example:

$scope.addClient = function(newClient) {

console.log(newClient);

  if(!newClient.name ||!newClient.email) {
  $rootScope.$emit('errorEvent',
     {"message" : "You must enter some client name"} 
    );
  };

...performing other functions...

}

Secondly I would like to use a variable in the error message so that I can tailor it to the value that is undefined.

2 Answers 2

1

you did not attach $scope to variable.

use this

    $scope.addClient = function(newClient) {

    if(!newClient){ 
       $rootScope.$emit('errorEvent',
         {"message" : "You must enter some client name"});
    }

    else if(!$scope.newClient.name || !scope.newClient.email) {
      $rootScope.$emit('errorEvent',{"message" : "You must enter some client name" });
     }
  }

or

$scope.addClient = function(newClient) {

     if(newClient && (!$scope.newClient.name || !scope.newClient.email) || !newClient ){ 
               $rootScope.$emit('errorEvent',
                 {"message" : "You must enter some client name"});
            }

          }
Sign up to request clarification or add additional context in comments.

4 Comments

I think event propagation is not working.replace $emit to $broadcast.
I just get an error on !newClient.name cannot read property of undefined
It mean that newClient is not define.please console in function and see , is it logs?
That is what I am trying to do... newClient will be undefined unless I enter something into the input.
0

I ended up having to do this:

    if(!client){ 
       $rootScope.$emit('errorEvent',
         {"message" : "You must enter a client name"});
    }

    else if(client.name === undefined) {
      $rootScope.$emit('errorEvent',{"message" : "You must enter a client name" });
     }

    else if(client.email === undefined ) {
      $rootScope.$emit('errorEvent',{"message" : "You must enter a client email" });
     }

    else { 

REST OF FUNCTION

}

Happy to receive constructive criticism / options to reduce this code...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.