AngularJS Declaring Multiple Controllers in 1 app

user2828701

I am having trouble declaring 2 controllers in 1 app. My guess is it's a syntax error, but not totally sure.

The error message I receive is that "people is not defined".

//this is my app file

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


 myApp.config(function($interpolateProvider) {
 $interpolateProvider.startSymbol('{[{');
 $interpolateProvider.endSymbol('}]}');
 });



   myApp.controller('controller1', function($scope) {

   var pets = [
   {animal: "dog", years: "3 Years"},
   {animal: "dog", years: "1 Years"},
   ];});


   myApp.controller('controller2', function($scope) {


   var people = [
   {name: "john", bday: "september"},
   {name: "nancy",bday: "december"},
   ];
   });

On my html page, I am simply writing some javascript that attempts to get the length of the people array.

  var totalpeople = people.length;

Note: Not sure if this is important information, but I declare my app in the html tag.

update: here some some basic pseudo code on my html page.

    <html ng-app="myApp">
    <head>
    <script>
    var totalpeople = people.length;
    </script>
    </head>
    <body>
    <div ng-controler="controller1">
    <table><tr ng-repeat="rows in pets">
    <td>{[{pets.animal}]}</td><td>{[{pets.year}]}</td>
    </tr>
    </div>
    <div ng-controler="controller2">
    <table><tr ng-repeat="rows in people">
    <td>{[{pets.name}]}</td><td>{[{pets.bday}]}</td>
    </tr>
    </div>
    <div>
    <table><tr>
    <td><script>//not actually doing this, but totalpeople goes here </script></td>
    </tr>
    </div>

    </body>
Sunil Lama

There were a lots of errors in the html as well as in the script section. Like you have mispelled the ng-controller to ng-controler. And, ng-repeat logic is all wrong.You have missing table tags and more. I don't know why you would want to interpolate {{}} to use {[{}]}.

Here is the working plunker: http://plnkr.co/edit/qs7mCrmElXGqGHdKAmNH?p=preview

    <html>
        <head>
          <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
        </head>
        <body ng-app="myApp">
        <div ng-controller="controller1">
            <table><tr ng-repeat="rows in pets">
            <td>{[{rows.animal}]}</td><td>{[{rows.years}]}</td>
                </tr>
                </table>
              Total pets:{[{count}]}
        </div>
        <div ng-controller="controller2">
        <table><tr ng-repeat="rows in people">
        <td>{[{rows.name}]}</td><td>{[{rows.bday}]}</td>
        </tr>
        </table>
        </div>
         <br>
                  Total Pets:
                 <span id="show"></span>
    <script>
    var myApp = angular.module('myApp',[]);


     myApp.config(function($interpolateProvider) {
     $interpolateProvider.startSymbol('{[{');
     $interpolateProvider.endSymbol('}]}');
     });



       myApp.controller('controller1', function($scope) {

       $scope.pets = [
       {animal: "dog", years: "3 Years"},
       {animal: "dog", years: "1 Years"}
       ];
       $scope.count = $scope.pets.length;
       document.getElementById('show').innerText=$scope.count;//use directive to achieve this,using jquery inside controller is bad practise. Used just for the purpose of demo.
});


       myApp.controller('controller2', function($scope) {
       $scope.people = [
       {name: "john", bday: "september"},
       {name: "nancy",bday: "december"},
       ];
       });
    </script>
        </body>
      </html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Declaring controllers in AngularJS

From Java

Multiple controllers with AngularJS in single page app

From Dev

RequireJS and AngularJS multiple controllers

From Dev

Splitting AngularJS controllers into multiple files

From Dev

AngularJS function available to multiple controllers

From Dev

Multiple controllers in angularJS throws error

From Dev

AngularJS factory with $http and multiple controllers

From Dev

AngularJS inject service into multiple controllers

From Dev

angularjs multiple controllers on one page

From Dev

Shared variables and multiple controllers AngularJS

From Dev

load jsonp into multiple AngularJS controllers

From Dev

AngularJS factory with $http and multiple controllers

From Dev

Declaring Multiple Singletons in 1 class

From Dev

Defer creation of controllers/services on app run angularjs

From Dev

Using Two Controllers in a Single AngularJS App

From Dev

Using Two Controllers in a Single AngularJS App

From Dev

I want to test controllers in AngularJS app

From Dev

Can I have 2 controllers in AngularJS app

From Dev

AngularJS submitting handling a form with multiple inner controllers

From Dev

Is it possible to apply multiple AngularJS controllers on the same element

From Dev

How to define controllers in multiple files - AngularJs

From Dev

How to define multiple controllers for one view in angularJS?

From Dev

AngularJS - single alert div for multiple controllers

From Dev

Initialize $scope variables for multiple controllers - AngularJS

From Dev

Updating the scope variable across multiple controllers in angularjs

From Dev

Angularjs service to manage dataset across multiple controllers

From Dev

AngularJS - single alert div for multiple controllers

From Dev

Kendo UI AngularJs Multiple Controllers on the Page

From Dev

How to use multiple AngularJS nested / related controllers

Related Related

  1. 1

    Declaring controllers in AngularJS

  2. 2

    Multiple controllers with AngularJS in single page app

  3. 3

    RequireJS and AngularJS multiple controllers

  4. 4

    Splitting AngularJS controllers into multiple files

  5. 5

    AngularJS function available to multiple controllers

  6. 6

    Multiple controllers in angularJS throws error

  7. 7

    AngularJS factory with $http and multiple controllers

  8. 8

    AngularJS inject service into multiple controllers

  9. 9

    angularjs multiple controllers on one page

  10. 10

    Shared variables and multiple controllers AngularJS

  11. 11

    load jsonp into multiple AngularJS controllers

  12. 12

    AngularJS factory with $http and multiple controllers

  13. 13

    Declaring Multiple Singletons in 1 class

  14. 14

    Defer creation of controllers/services on app run angularjs

  15. 15

    Using Two Controllers in a Single AngularJS App

  16. 16

    Using Two Controllers in a Single AngularJS App

  17. 17

    I want to test controllers in AngularJS app

  18. 18

    Can I have 2 controllers in AngularJS app

  19. 19

    AngularJS submitting handling a form with multiple inner controllers

  20. 20

    Is it possible to apply multiple AngularJS controllers on the same element

  21. 21

    How to define controllers in multiple files - AngularJs

  22. 22

    How to define multiple controllers for one view in angularJS?

  23. 23

    AngularJS - single alert div for multiple controllers

  24. 24

    Initialize $scope variables for multiple controllers - AngularJS

  25. 25

    Updating the scope variable across multiple controllers in angularjs

  26. 26

    Angularjs service to manage dataset across multiple controllers

  27. 27

    AngularJS - single alert div for multiple controllers

  28. 28

    Kendo UI AngularJs Multiple Controllers on the Page

  29. 29

    How to use multiple AngularJS nested / related controllers

HotTag

Archive