1

I have a text box where I enter an idNumber. I want to check if that id number appears in my table. If it does, I want to add a glyphicon to that row.

Now, my problem is that I don't know how to access ng-model="idNumber" from ng-if.

I try using ng-if="data.id== {{idNumber}}" but that does not work.

        <div class="container" id="inputDiv">
            <div>
                <input type="text" class="form-control" ng-model="idNumber">
            </div>
        </div>

        <div class="container col-md-12">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>Id number</th>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Status</th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="data in idData">
                    <td>{{data.id}}</td>
                    <td>{{data.name}}</td>
                    <td>{{data.dokumenttype}}</td>
                    <td>
                      <span ng-if="data.id== {{idNumber}}">
                        <span class="glyphicon glyphicon-asterisk"></span>
                      </span>
                    </td>                    
                </tr>
                </tbody>
            </table>
        </div>
chichi
  • 207
  • 1
  • 4
  • 13

2 Answers2

2

Use like this:

<span ng-if="data.id === idNumber">
1

You do not need to put an expression here, just replace it as,

<span ng-if="data.id===idNumber">
    <span class="glyphicon glyphicon-asterisk"></span>
</span>

DEMO

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

myApp.controller('thecontroller',function($scope){
   $scope.idData = [{
  "id": "1",
  "name": "Kimberlyn",
  "documentType": "McGaw"
}, {
  "id": "2",
  "name": "Harmony",
  "documentType": "Sedworth"
}, {
  "id": "3",
  "name": "Adela",
  "documentType": "Blenkin"
}];
});
<!DOCTYPE html>
<html>
    <head>
      <title>ng-Messages Service</title>
      <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
        <script src='https://code.angularjs.org/1.5.0-rc.0/angular.min.js'></script>
    </head>
    <body ng-app="myApp">
        <div ng-controller='thecontroller'>
       <div class="container" id="inputDiv">
            <div>
                <input type="text" class="form-control" ng-model="idNumber">
            </div>
        </div>
         <div class="container col-md-12">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>Id number</th>
                    <th>Name</th>
                    <th>Type</th>
                    <th>Status</th>
                </tr>
                </thead>
                <tbody>
                <tr ng-repeat="data in idData">
                    <td>{{data.id}}</td>
                    <td>{{data.name}}</td>
                    <td>{{data.dokumenttype}}</td>
                    <td>
                      <span ng-if="data.id === idNumber">
                        <span>test</span>
                      </span>
                    </td>                    
                </tr>
                </tbody>
            </table>
        </div>
        </div>
    </body>
</html>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thank you, this IS working! My problem was that I had this line in my controller: `$scope.idNumber= '';` – chichi Jul 04 '17 at 17:29