Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display content based on dropdown selected value in Angular

Posted on 2014-01-11
2
Medium Priority
?
10,615 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Find out what you should include to make the best professional email signature for your organization.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

963 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