Solved

Display content based on dropdown selected value in Angular

Posted on 2014-01-11
2
8,494 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
ID: 39773374
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
ID: 39775974
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

830 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