Solved

Display content based on dropdown selected value in Angular

Posted on 2014-01-11
2
7,723 Views
Last Modified: 2014-01-14
Hi Experts,

I am newbie to Angular and have a requirement to show the content based on dropdown value in Angular.I have following dropdown  values

None: show no content
Name: Show "Name" content only
Address:Show "Address" content only
All: Show both "Name" and "Address" contents.

I have below code developed in Visual Studio editor which is  working fine (UI screenshots attached ) using Angular,Bootstrap and HTML5.

I need sample working code for my requirement.

<div class="container" >

  <div style="padding:50px;" ng-controller="samplecontroller">

 <div class="row">
     <label class="col-lg-2">
         Status</label>
     <div class="col-lg-5">
         <select class="input-sm">
             
             <option selected value="none">None</option>
             <option value="name">Name</option>
             <option value="address">Address</option>
             <option value="all">All</option>
         </select>
     </div>
 </div>
 </br>
    
  <div class="row">
         <label class="col-lg-2">
             Name</label>
         <div class="col-lg-5">
             <p class="form-group">
                 {{name}}</p>
         </div>

     </div>
 </br>
     
     <div class="row">
                <label class="col-lg-2">
                     Address</label>
                 <div class="col-lg-5">
                    <p class="form-group">
                        {{address}}</p>
                  </div>

       </div>

   </div>

 </div>

Open in new window


<script type="text/javascript">
        function samplecontroller($scope) {
            $scope.name = "FOO";
            $scope.address = "ADDRESS1";
      
        
        }

Open in new window


Thanks in Advance
image1.JPG
image2.JPG
0
Comment
Question by:ksd123
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 300 total points
Comment Utility
Test page : http://jsfiddle.net/vKfjg/

<div class="container" ng-app>
    <div style="padding:50px;" ng-controller="samplecontroller">
        <div class="row">
            <label class="col-lg-2"> Status</label>
            <div class="col-lg-5">
                <select class="input-sm" ng-model="state" ng-options="s.value for s in status"></select>
            </div>
        </div>
        <br/>
        <div class="row" ng-hide="state.value!='name'&&state.value!='all'">
            <label class="col-lg-2">Name</label>
            <div class="col-lg-5">
                <p class="form-group">{{name}}</p>
            </div>
        </div>
        <br/>
        <div class="row" ng-hide="state.value!='address'&&state.value!='all'">
            <label class="col-lg-2">Address</label>
            <div class="col-lg-5">
                <p class="form-group">{{address}}</p>
            </div>
        </div>
    </div>
</div>

Open in new window


function samplecontroller($scope) {
    $scope.status = [
        {value:'none',text:'none'},
        {value:'name',text:'Name'},
        {value:'address',text:'Address'},
        {value:'all',text:'All'}
    ];
    $scope.state = $scope.status[0];
    $scope.name = "FOO";
    $scope.address = "ADDRESS1";
}

Open in new window

0
 

Author Comment

by:ksd123
Comment Utility
Thank you. I need "text" values in dropdown so I have changed below code from s.value to s.text

 <select class="input-sm" ng-model="state" ng-options="s.text for s in status"></select>

Open in new window


In reality, I have 6 dropdown values and 5 divs in a page layout and based on selection  I am displaying divs (none,1,2,3 etc). So the above approach (using ng-hide declaratively on each div)  is the best practice in angular ?

Thanks in Advance
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now