1

从我的解析中获取帐户和人员数组后,我如何访问组件控制器中的人员和帐户?我还尝试了在主 ctl 中定义 acctTally 并将其绑定到组件,但没有成功。

我可以将人员和帐户绑定到组件并在组件模板中访问它,但是我想在组件控制器中的任一数组上进行工作是我遇到问题的地方。我错过了什么关键概念????

主控制器

 angular.module('hellosolarsystem')
  .controller('AcctCtrl', function($scope, accounts, people){
    $scope.accounts = accounts;
    $scope.people = people;
  });

主模板

<nav-bar></nav-bar>
<acct-list people="people" accounts="accounts"></acct-list>

零件

function aCtrl(){
        var ctrl = this;
        ctrl.acctTally = [];
         ctrl.uniqueAcct = [];

         //Array of all accounts
      $scope.people.data.forEach(function(person){
           person.account_types.forEach(function(account){
             ctrl.acctTally.push(account.name);
           })
        });
        }

angular.module('hellosolarsystem').component('acctList', {
  bindings: { accounts: '<',
              people: '<'
            },
  controller: aCtrl,


  templateUrl: 'javascripts/app/components/accounts/acctsList/index.html'
})

组件模板

<table class = "table">
        <thead>
          <tr>
            <th>Accounts</th>
            <th>Number of Accounts Assigned Users</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat = "acct in $ctrl.acctTally">
            <td>{{acct.name}}</td>
            <td>{acct.tally}}<</td>
            <td>
              <button class = "btn btn-info" ng-click = "editUser($index)">Edit</button>
              <button class = "btn btn-danger" ng-click = "deleteUser($index)">Delete</button>
            </td>
          </tr>
        </tbody>
      </table>
4

1 回答 1

1

自 AngularJS 1.6 版本以来,当您的控制器函数被实例化时,组件的绑定不可用。在此处检查重大更改$onInit与 Angular 2+ 不同,当调用钩子时,绑定将可用。当控制器通过做实例化时,即使您可以强制执行预填充绑定的旧行为

.config(function($compileProvider) {
    $compileProvider.preAssignBindingsEnabled(true);
})

但是 Angular 团队非常不鼓励这样做。

根据 1.6.0 的重大更改,您必须移动代码以$onInit挂钩解决您的问题。

ctrl.$onInit = function() {
 ctrl.people.data.forEach(function(person){
    person.account_types.forEach(function(account){
       ctrl.acctTally.push(account.name);
    })
 });
}
于 2017-08-16T21:07:04.997 回答