Angular App中的多个控制器无法正常工作

时间:2015-03-19 14:32:37

标签: javascript angularjs angularjs-scope

我在同一个应用程序中有两个控制器,但似乎只有一个被注册:

var app = angular.module('MyModule', []);

app.controller('FruitCtrl', function($scope) {
    $scope.fruits = ['apples', 'oranges', 'bananas'];
});

app.controller('VegetableCtrl', function($scope) {
    $scope.vegetables = ['potato', 'beans', 'onions'];
});

我的模板:

<div ng-app="MyModule">

    <div ng-controller="FruitCtrl">
        <div ng-repeat="fruit in fruits">{{fruit}}</div>
    </div>

    <div controller="VegetableCtrl">
        <div ng-repeat="vegetable in vegetables">{{vegetable}}</div>
    </div>

</div>

只有&#34;水果&#34;部分正在呈现。

另见JS小提琴http://jsfiddle.net/doque/9wfhsgqf/2/

5 个答案:

答案 0 :(得分:3)

controller="VegetableCtrl"

应该是

ng-controller="VegetableCtrl"

答案 1 :(得分:2)

你错过了蔬菜重复的ng-controller控制器

http://jsfiddle.net/9wfhsgqf/3/

<div ng-app="MyModule">

<div ng-controller="VegetableCtrl">
    <div ng-repeat="vegetable in vegetables">{{vegetable}}</div>
</div>

<div ng-controller="FruitCtrl">
    <div ng-repeat="fruit in fruits">{{fruit}}</div>
</div>

答案 2 :(得分:1)

改变
<div controller="VegetableCtrl">

<div ng-controller="VegetableCtrl">

答案 3 :(得分:1)

替换

<div controller="VegetableCtrl">

<div ng-controller="VegetableCtrl">

答案 4 :(得分:1)

你错过了&#39; ng - &#39;从第二个div开始,它应该是&#39; ng-controller&#39;不只是控制器