AngularJS在1个app中声明多个控制器

时间:2016-02-25 14:54:46

标签: javascript angularjs

我无法在1个应用中声明2个控制器。我的猜测是语法错误,但不完全确定。

我收到的错误信息是“未定义人员”。

//这是我的app文件

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"},
   ];
   });

在我的html页面上,我只是在编写一些试图获取人员数组长度的javascript。

  var totalpeople = people.length;

注意:不确定这是否是重要信息,但我在html标记中声明了我的应用程序。

更新: 这里我的html页面上有一些基本的伪代码。

    <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>

3 个答案:

答案 0 :(得分:1)

html和脚本部分都有很多错误。就像你错误地将ng-controller误导为 ng-controler 一样。并且,ng-repeat逻辑都是错误的。您缺少表标签等等。我不知道您为什么要插入 {{}} 来使用 {[{}]}

以下是工作人员: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>

答案 1 :(得分:0)

  

在我的html页面上,我只是在写一些尝试的javascript   得到人数组的长度。

您确定期望people在全局范围内可用(即window对象的属性)吗?

现在让我们按如下方式简化代码:

function doStuff() {
   var people = "whatever";
}

doStuff();

// Is "people" variable available?

答案是,它不可用,因为它是一个doStuff函数局部变量。

这不是特定于Angular的问题,但它是变量作用域在JavaScript(以及几乎任何语言)中的工作原理。

实际上,Angular会尝试避免将 garbage 添加到全局范围,您需要使用内置数据绑定在UI中显示值。

答案 2 :(得分:0)

为什么你需要在视图中编写javascript来获取长度?

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

然后可以在你的视图中执行people.length。

看到你试图调用它的上下文会有所帮助。