Render New Views Within a Div Based on Click of Button | AngularJS

ajl123

Hi I am trying to render a new view based on clicks of a button in a navbar. However, it is not working.

Here is my HTML Code:

            <!-- Links to all the pages with data entry forms -->
            <div id="data_links" ng-controller="MainCtrl">
                <ul>
                    <li>
                        <a href="#" title="Home Page" ng-click = "ChangeView('instructions')"> Home </a>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Modify views here -->
        <div class="main" ng-controller="MainCtrl">
            <main role="main">
                <div ng-view> <!-- Where we will inject html --> </div>
            </main>
        </div>

<!-- Application Files -->
<script src="js/app.js"></script>
<script src="js/controllers/main.js"></script>

Here is my app.js:

angular.module('DataEntry', [
  'ngRoute'
]).config(function ( $routeProvider ) {
  $routeProvider
    .when('instructions', {
      templateUrl: 'views/instructions.html',
      controller: 'MainCtrl'
    });
});

Here is my controller main.js:

angular.module('DataEntry')
  .controller('MainCtrl',
    function MainCtrl ( $scope, $location ) {
      'use strict';
      $scope.ChangeView = function(view) {
        alert(view);
        $location.url(view);
      }
    });

This is my instructions.html page for testing to see if it loads:

<div class="module instructions">
    <div class="module_instructions">
        <h1> Testing Loaded! </h1>
        <p> Test <br> 
            Test <br> Test <br> Test 
        </p>
    </div>
</div>

I want to eventually be able to click on multiple links within a navbar, such as home, instructions, etc. and render different views within the ng-view section. However, it is not working right now and I am not sure how to scale it so I can add more .html pages for the different views I want to render. Can someone help me move forward?

lwalden

This line <a href="#" title="Home Page" ng-click = "ChangeView('instructions')"> Home

Can be changed to this:

<a href="/instructions" title="Home Page"> Home </a>

You don't need to use a function on your controller to set the url, although you can navigate this way - sometimes you want to redirect the user programatically and this works for those cases.

Also, leaving href="#" in that line is causing you a problem. In a non-angular page # is used as a href placeholder but on an Angular href="#" is actually going to be picked up by $routeProvider which will try to change the contents of the ng-view container. What loads will depend upon how you set up your .config section, but is generally not desirable behavior.

As you create more pages, add paths to your .config section and you can link to them from your html the same way as I did above with the /instructions path.

Here's an example:

angular.module('DataEntry', ['ngRoute'])
.config(function ( $routeProvider ) {
  $routeProvider
   .when('instructions', {
      templateUrl: 'views/instructions.html',
      controller: 'MainCtrl'
   })
   .when('faq', {
      templateUrl: 'views/faq.html',
      controller: 'FaqCtrl'
   })
   .when('example', {
      templateUrl: 'views/example.html',
      controller: 'ExampleCtrl'
   })
});

and in your markup:

<a href="/instructions" title="Home Page"> Home </a>

<a href="/faq" title="FAQ"> FAQ</a>

<a href="/example" title="Example Page"> Example</a>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert/Remove templates/views on button click in angularjs

From Dev

On click check the closest radio button within div

From Java

Open a new tab on button click in AngularJS

From Dev

angularjs Click button to show next / previous div

From Dev

Click button to copy text to another div with angularjs

From Dev

How to add div with a button click in angularJs

From Dev

Move to specific div based on button click

From Dev

On click of a button display different partial views in a particular div on condition

From Dev

Changing views on button click?

From Dev

Issue with views on button click

From Dev

Jasmine Custom Boot Within AngularJS - Executing Tests On Button Click

From Dev

AngularJS: show other div and hide current div on click of a button

From Dev

view detail in new page when click button using angularjs

From Dev

How does Android render Views within a Layout?

From Dev

How to open a url in webview on new screen based on button click

From Dev

show one div and hide others on button click in angularjs

From Dev

AngularJs Render div onclick

From Dev

AngularJs Render div onclick

From Dev

Render AngularJS directive within jqxGrid

From Dev

How to add a new textbox in the div when I click kendo button?

From Dev

Add a new div on button click and option to select an option to show textbox

From Dev

How to hide and show div based on multiple radio button click?

From Dev

how to show/hide Div on click event based on the value of button

From Dev

Hide Div based on button click asp.net

From Dev

Render partial view on button click

From Dev

button not staying within div

From Dev

Centering <a> button within a div

From Dev

Button Positioning within div

From Dev

Angularjs action on click of button

Related Related

  1. 1

    Insert/Remove templates/views on button click in angularjs

  2. 2

    On click check the closest radio button within div

  3. 3

    Open a new tab on button click in AngularJS

  4. 4

    angularjs Click button to show next / previous div

  5. 5

    Click button to copy text to another div with angularjs

  6. 6

    How to add div with a button click in angularJs

  7. 7

    Move to specific div based on button click

  8. 8

    On click of a button display different partial views in a particular div on condition

  9. 9

    Changing views on button click?

  10. 10

    Issue with views on button click

  11. 11

    Jasmine Custom Boot Within AngularJS - Executing Tests On Button Click

  12. 12

    AngularJS: show other div and hide current div on click of a button

  13. 13

    view detail in new page when click button using angularjs

  14. 14

    How does Android render Views within a Layout?

  15. 15

    How to open a url in webview on new screen based on button click

  16. 16

    show one div and hide others on button click in angularjs

  17. 17

    AngularJs Render div onclick

  18. 18

    AngularJs Render div onclick

  19. 19

    Render AngularJS directive within jqxGrid

  20. 20

    How to add a new textbox in the div when I click kendo button?

  21. 21

    Add a new div on button click and option to select an option to show textbox

  22. 22

    How to hide and show div based on multiple radio button click?

  23. 23

    how to show/hide Div on click event based on the value of button

  24. 24

    Hide Div based on button click asp.net

  25. 25

    Render partial view on button click

  26. 26

    button not staying within div

  27. 27

    Centering <a> button within a div

  28. 28

    Button Positioning within div

  29. 29

    Angularjs action on click of button

HotTag

Archive