【问题标题】:Multiple ng-controller in Angular.js is not workingAngular.js 中的多个 ng-controller 不起作用
【发布时间】:2015-10-12 04:16:38
【问题描述】:

我对一个模块使用 2 个控制器感到震惊。我想根据为模块定义的控制器执行单独的操作。对于第二个控制器,我没有得到预期的输出。

请发现以下方法有意义。

var helloApp = angular.module("helloApp", []);
helloApp.controller("CompanyCtrl", function($scope) {
$scope.companies = [
        { 'companyName':'Infi',
            'Level': 1,
            'Created': 'Bangalore','Status': 'In Progress'},
            { 'companyName':'Cogi',
            'Level': 1,
            'Created': 'Bangalore','Status': 'In Progress'},
                { 'companyName':'Infosys Technologies',
            'Level': 1,
            'Created': 'Bangalore','Status': 'In Progress'},
                    { 'companyName':'Infosys Technologies',
            'Level': 1,
            'Created': 'Bangalore','Status': 'In Progress'},
                        { 'companyName':'Infosys Technologies',
            'Level': 1,
            'Created': 'Bangalore','Status': 'In Progress'},
        ];


});

helloApp.controller("SectionController", function($scope) {
$scope.rightsection = [
        { 'Topic':'Bobs Pizza',
            'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache ' },
            { 'Topic':'Bill`s Jugs',
            'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '},
                { 'Topic':'Bobs Pizza',
            'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '},
                    { 'Topic':'Bill`s Jugs',
            'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '},
                        { 'Topic':'Bobs Pizza',
            'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '}
        ];

});

当我尝试使用第二个控制器时,(下)我没有看到页面上显示任何内容。上面的方法有错吗?如果是这样,如何将两个控制器添加到单个模块?请帮帮我。

<div class="right" ng-controller="SectionController">
<table class="table" id="tl" ng-repeat="section in rightSection">
<tr>
<th>Company Name
</th>

</tr>
<tr>
<td>{{section.Topic}}
</td>

</tr>
</table>
</div>

【问题讨论】:

  • 嘿,班加罗尔人,当你的作用域变量被称为 rightsection 时,你在 rightSection 上调用 ng-repeat

标签: javascript jquery angularjs angular-ui-bootstrap


【解决方案1】:

您对rightSection 的迭代和您的范围变量是rightsection(区分大小写)。

【讨论】:

  • 谢谢,费尔南多。但这是为单个模块创建多个控制器的正确且可接受的方式吗?
  • 是的,对于模块的每个控制器,您将调用 module.controller(...);
【解决方案2】:

看这个。在 div 中,您不会将 ng-app 属性和更改 de var 范围名称添加到 rightSection。

var helloApp = angular.module("helloApp", []);

helloApp.controller("CompanyCtrl", function($scope) {
  $scope.companies = [{
    'companyName': 'Infi',
    'Level': 1,
    'Created': 'Bangalore',
    'Status': 'In Progress'
  }, {
    'companyName': 'Cogi',
    'Level': 1,
    'Created': 'Bangalore',
    'Status': 'In Progress'
  }, {
    'companyName': 'Infosys Technologies',
    'Level': 1,
    'Created': 'Bangalore',
    'Status': 'In Progress'
  }, {
    'companyName': 'Infosys Technologies',
    'Level': 1,
    'Created': 'Bangalore',
    'Status': 'In Progress'
  }, {
    'companyName': 'Infosys Technologies',
    'Level': 1,
    'Created': 'Bangalore',
    'Status': 'In Progress'
  }, ];


});

helloApp.controller("SectionController", function($scope) {
  $scope.rightSection = [{
    'Topic': 'Bobs Pizza',
    'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '
  }, {
    'Topic': 'Bill`s Jugs',
    'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '
  }, {
    'Topic': 'Bobs Pizza',
    'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '
  }, {
    'Topic': 'Bill`s Jugs',
    'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '
  }, {
    'Topic': 'Bobs Pizza',
    'Comment': 'Raw denim you probably havent heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache '
  }];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="right" ng-app="helloApp" ng-controller="SectionController">
  <table class="table" id="tl" ng-repeat="section in rightSection">
    <tr>
      <th>Company Name
      </th>

    </tr>
    <tr>
      <td>{{section.Topic}}
      </td>

    </tr>
  </table>
</div>

【讨论】:

    猜你喜欢
    • 2018-04-08
    • 2018-03-27
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 2012-10-10
    相关资源
    最近更新 更多