0

I have list with some persons, and if this person have comment field - you can edit this information in input:text. So I show this field when person have this field. But I have a problem, when I remove previous comment - input is hide. It's couse person.comment = "", and I think it meens like false.

<input person="text" ng-show='person.comment' ng-model='person.comment'>

i try to do this thing:

ng-show='person.comment || person.comment === ""'

but perhaps exist a different way? like comment in person? My plnkr.

YoroDiallo
  • 345
  • 3
  • 6
  • 26
  • looks like your attempt does solve the problem if I understand this correctly. In the plunker, if I add `ng-show='person.comment || person.comment === ""'` the input field does not disappear on a deletion of the text in the input field. – Matthias Sep 05 '16 at 16:13
  • @Matthias yes it is, but sometimes (not often) person.comment - undefined, so I need to make `ng-show='person.comment || person.comment === ""' || person.comment === undefined` and this looks ugly, so perhaps we have a better way to check. like we do in `if` `if(comment in person)` for example – YoroDiallo Sep 05 '16 at 16:16
  • in what situation do you actually want to hide the input field? could use that, and show it in all other cases – Matthias Sep 05 '16 at 16:17
  • @Matthias I use it in plnkr, Jake and Jane has no fields, couse they has no param comment in they object – YoroDiallo Sep 05 '16 at 16:19
  • in that case, `comment in person` should indeed be what you want. – Matthias Sep 05 '16 at 16:34
  • @Matthias but It's not working in ng-model.. – YoroDiallo Sep 05 '16 at 16:35
  • did you add the quotes? `ng-show="'comment' in person"` see http://stackoverflow.com/questions/455338/how-do-i-check-if-an-object-has-a-key-in-javascript – Matthias Sep 05 '16 at 16:36
  • 1
    or better might be `person.hasOwnProperty('comment')` – Matthias Sep 05 '16 at 16:38
  • @Matthias yes I miss quotes) but hasOwnPropety - is better way! you can write your answer and I will vote) thx – YoroDiallo Sep 05 '16 at 16:53

2 Answers2

1

To check if an object has a key in javascript, for you, you could write:

ng-show="person.hasOwnProperty('comment')"

from How do I check if an object has a key in JavaScript?

Community
  • 1
  • 1
Matthias
  • 3,160
  • 2
  • 24
  • 38
0

Add Simply angular.forEach loop on $scope.stuff array Like:-

Js

var app = angular.module('App', []);

app.controller('Ctrl', function($scope) {
    $scope.hideVariable = true;
    $scope.stuff = [
      {
       name: 'Jack',
       age : 22,
       comment : 'good boy'
      },
      {
       name: 'Bob',
       age : 23,
       comment : 'likes beer'
      },
      {
       name: 'Alisa',
       age : 21,
       comment : 'pretty girl'
      },
      {
       name: 'Jane',
       age : 25,
       comment : "she's fine"
      },
      {
       name: 'Mike',
       age : 19,
       comment : 'playing guitar'
      }
    ]
   angular.forEach('$scope.stuff', function (data) {
      if(data.comment === null || data.comment === 'undefined' ) {
        $scope.hideVariable = false;
      }
   })
})

HTML

<input person="text"ng-show="hideVariable" ng-model='person.comment'>

Go through this plunker

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41