2014-12-24 37 views
0

我正在開發基於AngularJS的應用程序。現在我有兩個不同控制器的表格。但只有一個控制器正在工作。控制器是彼此的副本,但有小的差異。AngularJS無法同時獲得兩個控制器工作

如果我從代碼中刪除控制器B,則控制器A正在工作。
如果我從代碼中刪除控制器A,則控制器B正在工作。

兩個控制器都有一個自己的js文件。作爲最後加載的控制器始終是正在工作的控制器。

兩個控制器不能同時工作。我在控制檯中也遇到了這個錯誤。

Error: [ng:areq] http://errors.angularjs.org/1.2.28/ng/areq?p0=aCtrl&p1=not%20aNaNunction%2C%20got%20undefined

下面我的代碼,剝離,因爲它是很多來郵!我也不能得到的jsfiddle與所有的代碼工作(得多依賴)

aCtrl

(function() { 
    'use strict'; 
    angular.module('app.tables', []).controller('aCtrl', [ 
    '$scope', '$filter', '$http', function($scope, $filter, $http) { 
     var init; 
     $http.get('/data/a.json').success(function(data) { 
     $scope.stores = data; 
     return init(); 
     }); 
     $scope.stores = [{}]; 
     // rest of the code 

    } 
    ]); 

}).call(this); 

bCtrl(複印件)

(function() { 
    'use strict'; 
    angular.module('app.tables', []).controller('bCtrl', [ 
    '$scope', '$filter', '$http', function($scope, $filter, $http) { 
     var init; 
     $http.get('/data/b.json').success(function(data) { 
     $scope.stores = data; 
     return init(); 
     }); 
     $scope.stores = [{}]; 
     // rest of the code 

    } 
    ]); 

}).call(this); 

爲HTML aCtrl

<div class="page page-table" data-ng-controller="aCtrl"> 
<table class="table table-bordered table-striped table-responsive"> 
    <tbody> 
    <tr data-ng-repeat="store in currentPageStores"> 
    <td>{{store.col1}}</td> 
    <td>{{store.col2}}</td> 
    <td>{{store.col3}}</td> 
    <td>{{store.col4}}</td> 
    </tr> 
    </tbody> 
</table> 
</div> 

HTML的bCtrl

<div class="page page-table" data-ng-controller="bCtrl"> 
<table class="table table-bordered table-striped table-responsive"> 
    <tbody> 
    <tr data-ng-repeat="store in currentPageStores"> 
    <td>{{store.col1}}</td> 
    <td>{{store.col2}}</td> 
    <td>{{store.col3}}</td> 
    <td>{{store.col4}}</td> 
    </tr> 
    </tbody> 
</table> 
</div> 

回答

3

這是模塊的再創造的情況下,我相信。您的通話

angular.module( 'app.tables',[])

創建一個模塊。由於在代碼中有兩個這樣的實例,所以模塊被創建兩次,第二個覆蓋第一個。刪除第二個聲明並將其更改爲:

angular.module('app.tables').controller('bCtrl', [ //note the declaration now does not have [] as second parameter

+0

太棒了!這樣做的工作! – Timo002

相關問題