1

Hi I want to do a simple angularjs filter so I have this:

      <input type="text" id="name" style="display:block" class="form-control" placeholder="By name" ng-model="search.student_name">
      <input type="text" id="code" style="display:none" class="form-control" placeholder="By code" ng-model="search.student_code">
      <input type="text" id="room" style="display:none" class="form-control" placeholder="By room" ng-model="search.student_room">
      <select name="filter" id="filter">
        <option value="name">Name</option>
        <option value="code">Code</option>
        <option value="room">Room</option>
      </select>

and the test script (doesn't works... Jquery conflict)

 <script type="text/javascript">

  if(document.getElementbyId('filter').value === 'code'){
    document.getElementbyId('name').style.display="none";
    document.getElementbyId('code').style.display="block";
  };

 </script>

The question is: there is another way to show and hide the selected input? Can I do it on the server-side?

3
  • did you look at Angular docs ng-ptions? [docs.angularjs.org/api/ng/directive/ngOptions] Commented Apr 17, 2015 at 13:28
  • in your if statement, you are comparing value with codigo. But there is nothing like that. It should be code, I suppose. Commented Apr 17, 2015 at 13:29
  • haha yeah @PrerakSola, I forgot to change it posting it here. Commented Apr 17, 2015 at 13:35

1 Answer 1

2

With AngularJS you can use NgShow to show items optionally.

<div ng-app>

<input type="text" id="name" style="display:block" class="form-control" placeholder="By name" ng-model="search.student_name" ng-show="filter =='name'">
      <input type="text" id="code" style="display:none" class="form-control" placeholder="By code" ng-model="search.student_code" ng-show="filter =='code'">
      <input type="text" id="room" style="display:none" class="form-control" placeholder="By room" ng-model="search.student_room" ng-show="filter =='room'">
      <select name="filter" id="filter" ng-model="filter">
        <option value="name">Name</option>
        <option value="code">Code</option>
        <option value="room">Room</option>
      </select>

</div>

I've added an ng-model to the select and an ng-show directive to each of the input fields. they will only show if the value of the select is equal to the literal in the ng-show

This assumes that angular is already referenced in the page.

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

Comments

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.