1

后,我得到了我的决心账户和人阵,我该如何访问人和账户组件控制器?我也尝试过在主ctl中定义acctTally,并将其绑定到组件,但没有运气。组件,在Angularjs1.5结合 - 从一个控制器将数据传递到另一个

在那里我遇到的问题,我可以将人们和户口只需找到的组件,并将其接入分量模板,但我想这样做在组件控制器或者阵列上的工作。我缺少什么关键概念?

主控制器

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> 

回答

1

绑定的组件时,因为AngularJS 1.6版本中,您的控制器功能被实例化没有用。检查breaking changes here。绑定将在$onInit挂钩被调用时与Angular 2+不同。即使当控制器被做

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

但上面做的角团队非常不鼓励实例化,你可以强制预填充绑定的旧行为。

每1.6.0重大的变动你有你的代码移到$onInit钩解决您的问题。

ctrl.$onInit = function() { 
ctrl.people.data.forEach(function(person){ 
    person.account_types.forEach(function(account){ 
     ctrl.acctTally.push(account.name); 
    }) 
}); 
} 
+1

非常感谢,我曾尝试使用$ onInit,但忘记将它附加到CTRL! – user5852854

相关问题