名称为"PokemonCtrl"的控制器未注册

DDe*_*gro 6 javascript controllers angularjs

我正在尝试将控制器添加到我的Angularjs应用程序中.

这是我第一次在不使用$scope依赖项的情况下进行设置,并使用路由来声明我正在使用的控制器.

PokemonCtrl没有注册的地方,我做错了什么?另外,如果我在路由中声明控制器这意味着我不必在其他任何地方声明它?

app.js

    'use strict';

/**
 * @ngdoc overview
 * @name pokedexApp
 * @description
 * # pokedexApp
 *
 * Main module of the application.
 */
angular
    .module('pokedexApp', [
        'ngAnimate',
        'ngCookies',
        'ngResource',
        'ngRoute',
        'ngSanitize',
        'ngTouch'
    ])
    .config(function($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl',
                controllerAs: 'main'
            })
            .when('/pokemon', {
                templateUrl: 'views/pokemon.html',
                controller: 'PokemonCtrl',
                controllerAs: 'controller'

            })
            .otherwise({
                redirectTo: '/main'
            });
    });
Run Code Online (Sandbox Code Playgroud)

pokemonCtrl.js

    'use strict';

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

pokedexApp.controller('PokemonCtrl', function() {
    var vm = this;

    vm.text = "Catch em All!"

});
Run Code Online (Sandbox Code Playgroud)

Leo*_*ana 24

问题是您要覆盖模块定义.当你写这行时:

var pokedexApp = angular.module('pokedexApp', []);
Run Code Online (Sandbox Code Playgroud)

你正在定义一个模块.如果再次调用它,使用相同的名称并传递一个空数组,则覆盖它.如果您只是想要检索模块,那么请继续使用

var pokedexApp = angular.module('pokedexApp');
Run Code Online (Sandbox Code Playgroud)