0

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>
asked Feb 25, 2016 at 14:54
6
  • show where you are calling people. Commented Feb 25, 2016 at 14:56
  • can you share you html as well, if you have plunker it will be easier Commented Feb 25, 2016 at 14:56
  • Your snippet isn't clear at all, provide a well-intentation. By the way, you can't put a comma on a last element in array. Commented Feb 25, 2016 at 15:00
  • You have not exposed people or pets in your controllers. You would want to declare them as this.people and this.pets respectively. Commented Feb 25, 2016 at 15:14
  • @Alex Rumba just updated some HTML Commented Feb 25, 2016 at 15:30

3 Answers 3

1

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>
answered Feb 25, 2016 at 15:38

5 Comments

@ Alex Rumba this makes sense. Sorry for the sloppy pseudo code, was trying to get something up quickly. Thank you. But how about my pets.length? I still want to reference that elsewhere in my HTML further down on my page.
okay updated the plunker for count as well. Does that satisfy your need?
ok - So I need to make sure I reference it within my controller area. Is there anyway I can reference it outside that area?
well you can either using jquery inside the controller which is not recommended, but will work. You should use directive for that purpose. I will simply give you an example using javascript inside controller
Sharing datas in between controllers has been discussed a lot in other threads. So, i suppose you won't be asking that. :)
0

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

Are you sure you expect people to be available in the global scope (i.e. a property of window object)?

Now let's simplify the code as follows:

function doStuff() {
 var people = "whatever";
}
doStuff();
// Is "people" variable available?

The answer is no, it's not available because it's a doStuff function local variable.

This isn't an Angular-specific issue, but it's how variable scoping works in JavaScript (and practically in any language).

Actually, Angular tries to avoid adding garbage to the global scope and you need to use built-in data-binding to show values in your UI.

answered Feb 25, 2016 at 15:00

Comments

0

Why would you need to write javascript in your view to get the length?

 $scope.people = [
 {name: "john", bday: "september"},
 {name: "nancy",bday: "december"},
 ];

Can then just do people.length in your view.

Seeing the context that you are trying to call it would help though.

answered Feb 25, 2016 at 15:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.