仍然得到Controller不是一个函数,得到了未定义,而我没有定义控制器的全局

时间:2015-11-26 02:51:36

标签: javascript angularjs ng-controller

仍然得到相同的错误Argument 'AveragesCtrl' is not a function, got undefined我已经尝试了类似问题提供的所有解决方案,但没有运气,任何我做错的想法?

<div >

    <div ng-controller="AveragesCtrl">

    <table>
      <tr ng-repeat="actor in avengers.cast">
        <td>{{actor.name}}</td>
        <td>{{actor.character}}</td>
      </tr>
    </table>

  </div>

  </div>

JS

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

    myApp.factory('Averages', function () {

        var Avengers = {};
        Avengers.cast = [
          {
            name: "Robert Downey Jr.",
            character: "Tony Stark / Iron Man"
          },
          {
            name: "Chris Evans",
            character: "Steve Rogers / Captain America"
          },
          {
            name: "Mark Ruffalo",
            character: "Bruce Banner / The Hulk"
          },
          {
            name: "Chris Hemsworth",
            character: "Thor"
          },
          {
            name: "Scarlett Johansson",
            character: "Natasha Romanoff / Black Widow"
          },
          {
            name: "Jeremy Renner",
            character: "Clint Barton / Hawkeye"
          },
          {
            name: "Tom Hiddleston",
            character: "Loki"
          },
          {
            name: "Clark Gregg",
            character: "Agent Phil Coulson"
          },
          {
            name: "Cobie Smulders",
            character: "Agent Maria Hill"
          },
          {
            name: "Stellan Skarsgard",
            character: "Selvig"
          },
          {
            name: "Samuel L. Jackson",
            character: "Nick Fury"
          },
          {
            name: "Gwyneth Paltrow",
            character: "Pepper Potts"
          },
          {
            name: "Paul Bettany",
            character: "Jarvis (voice)"
          },
          {
            name: "Alexis Denisof",
            character: "The Other"
          },
          {
            name: "Tina Benko",
            character: "NASA Scientist"
          }
        ];
        return Avengers;
    });



    myApp.controller("AvengersCtrl", function AvengersCtrl(Avengers) {
      var avengersCtrl = this;
        avengersCtrl.avengers = Avengers;
    }); 

1 个答案:

答案 0 :(得分:2)

没有控制器名称AveragesCtrl

您声明的控制器名称AvengersCtrl

转换此

<div ng-controller="AveragesCtrl">

<div ng-controller="AvengersCtrl">