在AngularJS中定义控制器的正确方法是什么?

问题描述:

我创造了AngularJS控制器,它工作正常,当我把它定义是这样的:在AngularJS中定义控制器的正确方法是什么?

aluPlanetApp.controller('KontaktController', function($scope) { 
    $scope.title= 'Kontakt'; 
    $scope.id = '10'; 
    $scope.users =[{"MIGX_id":"1","image":"upload/bridge.png"}]; 
}); 

然而,当我把它定义这样我得到一个错误:

(function() { 
    'use strict'; 

    aluPlanetApp.controller('KontaktController', KontaktController); 

    KontaktController.$inject = ['$scope']; 

    function KontaktController($scope) { 
     $scope.title= 'Kontakt'; 
$scope.id = '10'; 
$scope.users =[{"MIGX_id":"1","image":"upload/bridge.png"}]; 

     activate(); 

     function activate() { } 
    } 
})(); 

这是我得到的错误:

TypeError: aluPlanetApp.config(...) is not a function aluPlanetApp.config(function($routeProvider) {

整个JavaScript文件是here

+0

您从不声明'aluPlanetApp'作为角度模块,以使任一版本能够如所示那样工作。若有所失的第一个版本甚至工作 – charlietfl

+0

我已经宣布在主要页面这样 VAR aluPlanetApp = angular.module( 'aluPlanetApp',[ 'ngRoute', 'ngSanitize' , 'ui.bootstrap', '角传送带']); –

就完全摆脱aluPlanetApp变量,始终使用angular.module()

angular.module('aluPlanetApp').controller(... 
angular.module('aluPlanetApp').service(... 
angular.module('aluPlanetApp').directive(... 

,吸气版本见John Papa Angular Style Guide

+0

为约翰帕帕的参考....一个很好的资源。 – Thalaivar

你需要先经过“使用严格的”,让模块,

var aluPlanetApp = angular.module("aluPlanetApp");