4

I want to display some data and tables in the content div which depends on which category you choose on the left hand side navigation. So if I change the category also the displayed content of the content div should change.

Here is my code on Plunkr.

But it seems that not even this simple example is working.

So I have two Questions:

1.) How can I fix this example code to run ?

But more important:

2.) Is there a better way to change the content div when you change the category ?

5 Answers 5

4

I removed 'this' elements from code and also changed ng-view to ng-show.

<div>
  <div ng-show="showApple">{{content}}</div>
  <div ng-show="showBanana">{{content}}</div>
  <div ng-show="showOrange">{{content}}</div>
</div>

There was something wrong with that you named your div class "content" so I removed that also.

I am sure it isn't a perfect solution but now it works.

link to plnkr

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

1 Comment

It works and thanks for sharing but I can't help but think there's a fair bit of code just to do something really simple. JQuery tabs does all that with just this: $( "#tabs" ).tabs();
2

To be honest your best bet is to use $states/views. With a data-ui-view on the content div and a data-ui-sref link on the button on your menu, you can easily switch out content. Take a look at the ui-router page to get a better understanding of it. With templates for each 'view' that your menu will click to, your code will not just be much easier to manage, but probably more understandable.

2 Comments

but is it possible to use a ng-view directive inside another ng-view ? becase the page my content is shown is just a template loaded inside the ng-view
Yes you can, see this nested states/views link
2

You can use ng-include to show your contents but you have to keep them in seperate files e.g contentForApple, contentForBanana and contentForOrange. Here I can show you a little change in your div

  <div class="content">
    <div ng-show="MainCtrl.showApple" ng-include ="'contentForApple.html'"></div>
    <div ng-show="MainCtrl.showBanana" ng-include = "'contentForBanana.html'"></div>
    <div ng-show="MainCtrl.showOrange" ng-include = "'contentForOrange.html'"></div>
  </div>

Comments

0

I did it with simple ngif

1) Keep an Index of Page which needs to be is loaded now

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

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
   $scope.pageIndex = 0;

  /*
   * Updates current Page index
   */
  $scope.changeIndex= function(indexToChange){

       $scope.pageIndex = indexToChange;

    }
});

2) Load content based on selected index

  <body ng-controller="MainCtrl">

    <div class="mainframe">
        <div class="navigation">
            <ul class="list-group">
              <li class="list-group-item" ng-click="changeIndex(0)">Home<span class="status-icon"></span></li>
                <li class="list-group-item" ng-click="changeIndex(1)">Sign Up<span class="status-icon"></span></li>
                <li class="list-group-item" ng-click="changeIndex(2)">About<span class="status-icon"></span></li>
            </ul>
        </div>


        <div class="content">
       <div ng-if="pageIndex == 0" ng-include ="'Home.html'"></div>
       <div ng-if="pageIndex == 1" ng-include = "'SignUp.html'"></div>
       <div ng-if="pageIndex == 2" ng-include = "'About.html'"></div>
      </div>
  </div>

Final Result:
https://embed.plnkr.co/ic0eY2vwiOnChN2ahrEt/

Comments

0

Take one JSON array for category and data and get details of JSON data which index is clicked

<!DOCTYPE html>
<html>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


<body ng-app="myApp" ng-controller="MyController">
    
    <div class="container">
        <div class="row">
            <div class="col-lg-3">
                <ANY ng-repeat="x in category" ng-click="getData($index)">
                {{x.category}}<br>
                </ANY>
            </div>

            <div class="col-lg-6">
                {{data}}
            </div>
        </div>  
    </div>

<script>
var app = angular.module('myApp', []);

app.controller('MyController', function ($scope) 
{       
    $scope.data = '';
    $scope.category = [{category:'Apple',data:'Apple Data'},{category:'Banana',data:'Banana Data'},{category:'Orange',data:'Orange Data'}];

    $scope.getData = function(index)
    {
        $scope.data = $scope.category[index]['data'];
    }
});

</script>
</body>
</html>

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.