1

I'm trying to check if a string is empty in a form, setting a default value if the answer is true, or passing the actual value if the string isn't empty.
Here's my code from the controller:

$scope.addElem = function () {
    $scope.lista2.push({
        com: null ? com = 'VUOTO' : com = $scope.newItem.com,
        gruppo: null ? gruppo = 'VUOTO' : gruppo = $scope.newItem.gruppo
    });
};  

Here's the code from the HTML (I'm also using Bootstrap):

<form class="form-inline" name="input">
    <input type="text" class="form-control col-5" ng-model="newItem.com" placeholder="Nome del comico" ng-keypress="$event.keyCode == 13 && addElem()" />
    <input type="text" class="form-control col-5" ng-model="newItem.gruppo" placeholder="Gruppo del comico" ng-keypress="$event.keyCode == 13 && addElem()" />
    <button class="btn btn-outline-secondary btn-sm col-2" type="submit" ng-click="addElem()">Inserisci</button>
</form>
5
  • Where is newItem defined in your controller? Commented Mar 8, 2018 at 17:07
  • Maybe this will help you out. stackoverflow.com/questions/36869983/… Commented Mar 8, 2018 at 17:08
  • Or this: stackoverflow.com/questions/27380000/… . Commented Mar 8, 2018 at 17:09
  • @wmash it is defined in the ng-model Commented Mar 8, 2018 at 17:15
  • @JoeRazon the first one gives me a suggestion from Visual studio saying it's expected ':' instead of '.'; the second one doesn't solve my problem since i don't want to show something in the page but to pass a value that has to be put in a table Commented Mar 8, 2018 at 17:20

3 Answers 3

2

Your ternary condition is incorrectly built. Change it to something like below and it should work:

$scope.addElem = function () {
    $scope.lista2.push({
        com: $scope.newItem.com === null ? 'VUOTO' : $scope.newItem.com,
        gruppo: $scope.newItem.gruppo === null ? 'VUOTO' : $scope.newItem.gruppo
    });
};  
Sign up to request clarification or add additional context in comments.

5 Comments

now regardless of the content of the string in the table where these results are shown i got blank values
What do you mean?
i.imgur.com/I59EE4M.png the last line "is born" after i tried to send the random letters to the table
Just replace $scope.newItem.gruppo === null with $scope.newItem.gruppo.trim() and $scope.newItem.com === null with $scope.newItem.com.trim() in that condition and try again
using trim() gives error if com has value but gruppo doesn't and sets VUOTO-VUOTO if both are full
0

It seems like you were attempting to use ternary operators here to set the property of the object. The code below will set com to the value if $scope.newItem.com returns truthy or the default value of "VUOTO" if not. Same for gruppo.

$scope.lista2.push({
    com: $scope.newItem.com ? $scope.newItem.com : "VUOTO",
    gruppo: $scope.newItem.gruppo ? $scope.newItem.gruppo : "VUOTO",
});

7 Comments

i don't want to print (or in this case send to the table) 'VUOTO' if the string isn't empty. I want to pass 'VUOTO' if the string is empty, otherwise to pass the actual value of it
thank you very much, the solution works like a charm. so, in order to understand how it works, this code check if newItem.com has an actual value, if the condition is false it assigns the string VUOTO.Am I wrong?
You're welcome. And yes, you are correct in that. An empty string in JS evaluates as falsy. So yes, if the string is empty, "VUOTO" will be assigned as the value. If you are unfamiliar with ternary operators, you can read about them here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
sorry to bother you, but this code des not recognize the string in gruppo field. if only gruppo has something, in the table i got VUOTO-*blank*, if both are empty i got VUOTO-VUOTO but if both are full i got com's value-*blank
So the error is that if the <input> field for gruppo is populated with a value (string is not empty), that value is not in the table? What is the value you are putting into the gruppo field? Can you log $scope.newItem.gruppo and see that you have a value?
|
0

How about trimming data string?

$scope.addElem = function () {
  $scope.lista2.push({
    com: $scope.newItem.com.trim().length === 0 ? com = 'VUOTO' : com = 
  $scope.newItem.com,
    gruppo: $scope.newItem.gruppo.trim().length === 0 ? gruppo = 
    'VUOTO' : gruppo = $scope.newItem.gruppo
  });
};

1 Comment

with this solution if one of the input form is empty i got an Angular error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.