尝试使用angular-gridify时出现Angular Injector错误

时间:2015-09-06 16:58:44

标签: javascript angularjs

有一个jsFiddle here

我正在尝试以与this类似的方式使用angular-gridify,但是我得到了一个$ injector错误,如下所示。我应该如何注入angular-gridify依赖项,假设问题是什么?

Uncaught Error: [$injector:modulerr] Failed to instantiate module testApp due to:
Error: [$injector:nomod] Module 'testApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

HTML:

<body ng-app="ExampleApp" ng-controller="ExampleCtrl" ng-click="generateTiles()">
    <div class="gridify" ng-gridify="{wrapperSelector: '.wrapper', tileSelector: '.tile', perRow: 5, averageRatio: 1.5, gutter: 10, watch: 'tiles'}">
        <div class="wrapper">       
            <div ng-repeat="tile in tiles" class="tile" data-ratio="{{tile.ratio}}" style="background-color: {{tile.color}}">
                <img src="{{tile.src}}"></img>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
    <script type="text/javascript" src="angular-gridify.js"></script>
    <script type="text/javascript" src="app.js"></script>
</body>

JavaScript的:

var testApp = angular.module('testApp', ['ngRoute', 'angular-gridify']).config(function() {});
testApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
        controller: 'testCtrl'
    })
});
testApp.controller('testCtrl', function($scope) {
    $scope.tiles = [];

    $scope.generateTiles = function() {
        for (var i = 0; i < 50; i++) {
            var ratio = Math.random() > 0.6 ? 0.661 : 1.511;
            $scope.tiles[i] = {
                ratio: ratio,
                color: '#' + ('000000' + Math.floor(Math.random() * 16777215).toString(16)).slice(-6)
            };

        }
    };

    $scope.generateTiles();
});

2 个答案:

答案 0 :(得分:0)

在你的js中你注册了 testCtrl 控制器而不是 ExampleCtrl 控制器

答案 1 :(得分:0)

我也有时难以使用JSFiddle运行Angular应用程序。我拿了你的代码,修改了一下,然后把它放在Codepen中:

http://codepen.io/cgav/pen/QjweXe?editors=111

JS:

var testApp = angular.module('testApp', ['ngRoute', 'angular-gridify']);
testApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
        controller: 'testCtrl'
    })
});
testApp.controller('testCtrl', function($scope) {
    $scope.tiles = [];

    $scope.generateTiles = function() {
        for (var i = 0; i < 50; i++) {
            var ratio = Math.random() > 0.6 ? 0.661 : 1.511;
            $scope.tiles[i] = {
                ratio: ratio,
                color: '#' + ('000000' + Math.floor(Math.random() * 16777215).toString(16)).slice(-6)
            };

        }
    };

    $scope.generateTiles();
});

HTML:

<div ng-app="testApp" ng-controller="testCtrl">
  <div class="gridify" ng-gridify="{wrapperSelector: '.wrapper', tileSelector: '.tile', perRow: 5, averageRatio: 1.5, gutter: 10, watch: 'tiles'}">
      <div class="wrapper">
          <div ng-repeat="tile in tiles" class="tile" data-ratio="{{tile.ratio}}" style="background-color: {{tile.color}}">
          </div>
      </div>
  </div>
</div>

CSS:

body {
    margin: 0;
    padding: 10px;
}
.tile {
    width: 32px;
    height: 32px;
}
.tile > img {
    width: 100%;
    height: 100%;
}

这是你在找什么?

编辑: 请记住包括对angular-gridify和angular-route的引用。