使用ng-repeat时遇到麻烦,“MainCtrl不是函数,未定义”

时间:2015-05-28 22:11:13

标签: javascript angularjs

问题

我正在使用应用程序的基础创建一个Angular应用程序。我在使用ng-repeat将要从对象呈现的数据提取到模板home.html时遇到问题。我没有收到数据,而是在控制台中看到一条错误消息,它提供了{{business.name}}而不是实际数据。

错误消息

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined

我在下面包含了我的Github回购:

链接:https://github.com/onlyandrewn/angular

app.js

(function() {
  'use strict';

  angular.module('application', [
    'ui.router',
    'ngAnimate',

    //foundation
    'foundation',
    'foundation.dynamicRouting',
    'foundation.dynamicRouting.animations'
  ])
    .config(config)
    .run(run)
  ;

var myApp = angular.module('myApp',[]);
myApp.controller('MainCtrl', function($scope) {
    $scope.businesses = [{
           id: 0,
           name: "Andrew Nguyen",
           description: "I'm a web developer",
           address: "322 11th Street, Brandon, MB",
           website: "http://andrewnguyen.ca"
         },
         {
           id: 1,
           name: "Mary Yacoubian",
           description: "I'm a dental hygenist",
           address: "18 Wishford Drive",
           website: "http://quitecontrary.com"
         },
         {
           id: 2,
           name: "John Axon",
           description: "I'm a jack of all trades",
           address: "1101 Mississauga Rd.",
           website: "http://johnaxon.com"
         }];
  });

  config.$inject = ['$urlRouterProvider', '$locationProvider'];

  function config($urlProvider, $locationProvider) {
    $urlProvider.otherwise('/');

    $locationProvider.html5Mode({
      enabled:false,
      requireBase: false
    });

    $locationProvider.hashPrefix('!');
  }

  function run() {
    FastClick.attach(document.body);
  }

})();

index.html(ng-app =“application”)住在这里

<!doctype html>
<html lang="en" ng-app="application">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Brandon Sun Business Directory</title>
    <link href="/assets/css/app.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>

  </head>
  <body>
    <div class="grid-frame vertical">
      <div class="grid-content shrink" style="padding: 0;">
        <div class="primary condense menu-bar">
        <div class="logo">
          <img src="http://placehold.it/80x45" class="bdnsun" alt="">
          <div class="social">
            <a href="" class="click"><i class="fa fa-twitter"></i></a>
            <a href="" class="click"><i class="fa fa-facebook"></i></a>
          </div><!-- /.social -->
        </div><!-- /.logo -->
        </div>
      </div>
      <div ui-view class="grid-content">
      </div>
    </div>
    <script src="/assets/js/foundation.js"></script>
    <script src="/assets/js/templates.js"></script>
    <script src="/assets/js/routes.js"></script>
    <script src="/assets/js/app.js"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/2.2.0/isotope.pkgd.js"></script>
  </body>
</html>

home.html(ng-controller =“MainCtrl)住在这里

---
name: home
url: /
---

    <div class="grid-container">
        <div class="hero">
            <p class="sponsor">Sponsored by </p>
            <img src="http://placehold.it/200x30" class="sponsors" alt="">
            <h1>Business Directory</h1>
            <div class="find">
                <label for="looking">
                    <i class="fa fa-search"></i>
                </label>
                <input type="search" id="looking" placeholder="What are you looking for?">
                <input type="submit">
            </div><!-- /.find -->
            <ul>
                <li class="popular">Popular searches:</li>
                <li>tk-category <span>|</li>
                <li>tk-category <span>|</span></li>
                <li>tk-category <span>|</span></li>
                <li>tk-category <span>|</span></li>
                <li>tk-category </li>
            </ul>
        </div><!-- /.hero -->

        <div class="businesses">
            <p class="number">tk-number of businesses</p><button class="filter button">Filter by <i class="fa fa-chevron-down"></i></button>
            <div class="options">
                <div class="cat">
                    <div class="categories">
                        <div class="group">
                            <p class="name">Grade Level</p>
                            <div class="check">
                                <input type="radio" name=""><p>Auto</p>
                                <input type="checkbox" name=""><p>Restaurant</p>
                                <input type="checkbox" name=""><p>Other</p>
                            </div><!-- /.check -->
                        </div><!-- /.group -->

                        <div class="group">
                            <p class="name">School Type</p>
                            <div class="check">
                                <input type="checkbox">
                                <input type="checkbox">
                                <input type="checkbox">
                                <input type="checkbox">
                                <input type="checkbox">
                            </div><!-- /.check -->
                        </div><!-- /.group -->
                    </div><!-- /.categories -->
                </div><!-- /.cat -->
            </div><!-- /.options -->
        </div><!-- /.businesses -->



        <div class="buttons">
            <button class="alp">Alphabetically</button>
            <button class="exp">Expanded</button>
            <button class="con">Condensed</button>
        </div><!-- /.buttons -->

        <div class="grid-block small-up-3" ng-controller="MainCtrl">
            <div class="grid-block">
                <img src="http://placehold.it/300x300" class="storefront" alt="">
                    <p class="name">{{business.name}}</p>
                    <p class="description">{{business.description}}</p>
                    <p class="address">{{business.address}}</p>
                    <a href="" class="website">{{business.website}}</a>
            </div>

            <div class="grid-block">
                <img src="http://placehold.it/300x300" class="storefront" alt="">
                    <p class="name">{{business.name}}</p>
                    <p class="description">{{business.description}}</p>
                    <p class="address">{{business.address}}</p>
                    <a href="" class="website">{{business.website}}</a>
            </div>

            <div class="grid-block">
                <img src="http://placehold.it/300x300" class="storefront" alt="">
                    <p class="name">{{business.name}}</p>
                    <p class="description">{{business.description}}</p>
                    <p class="address">{{business.address}}</p>
                    <a href="" class="website">{{business.website}}</a>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:1)

Angular应用中没有名为“应用”的“ MainCtrl ”。

您已将 MainCtrl 定义为 MyApp 的一部分。这是复制/粘贴错误吗?

// Markup
ng-app="application">
    ...
    ng-controller="MainCtrl">


// JavaScript
var myApp = angular.module('myApp',[]);
myApp.controller('MainCtrl', function($scope) {