2014-04-16 4 views
0

각도를 사용하여 타이어를 차고 내 샌드 박스 앱을 실행하는 데 문제가 있습니다. 나는 서비스를 사용하여 간단한 컨트롤러를 구현하고 싶다. 그러나 다음과 같은 오류가 발생합니다.오류 : Argumentcontroller가 정의되지 않음

Error: Argument 'UserController' is not a function, got undefined

파일은 표시된 순서대로 페이지에서 참조됩니다. 앱이 여러 파일에 걸쳐 있기 때문에 webConfig.JS

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

UserController.JS

app.controller('UserController', function ($scope, UserService) { 
    // Define the model properties. The view will loop 
    // through the services array and genreate a li 
    // element for every one of its items. 

    $scope.services = [ 
     { 
      name: 'Web Development', 
      price: 300, 
      active: true 
     }, { 
      name: 'Design', 
      price: 400, 
      active: false 
     }, { 
      name: 'Integration', 
      price: 250, 
      active: false 
     }, { 
      name: 'Training', 
      price: 220, 
      active: false 
     } 
    ]; 

    $scope.LogInfo = { 
     UserName: 'username', 
     Password: 'password' 
    }; 

    $scope.ProcessLogin = function (Info) { 
     UserService.GetLoginStatus(Info); 
    }; 

    $scope.toggleActive = function (s) { 
     s.active = !s.active; 
    }; 

    // Helper method for calculating the total price 

    $scope.total = function() { 

     var total = 0; 

     // Use the angular forEach helper method to 
     // loop through the services array: 

     angular.forEach($scope.services, function (s) { 
      if (s.active) { 
       total += s.price; 
      } 
     }); 

     return total; 
    }; 
}); 

UserService.JS

app.service('UserService', function() { 
    this.GetLoginStatus = function (Info) { 
     alert(JSON.stringify(Info)) 
    }; 
}); 

답변

0

은 내가 app 통해 모듈에 컨트롤러를 추가 할 것 변수 대신 이름을 통해 모듈을 찾습니다. ...

변경 :

app.controller('UserController', function ($scope, UserService) { 
& 
app.service('UserService', function() { 

하려면 :

angular.module('app').controller('UserController', function ($scope, UserService) { 
& 
angular.module('app').service('UserService', function() { 
+0

@ 브론 코 제안에 감사하지만 같은 결과가 나옵니다. – CodeMilian

+0

파일을 축소하고 있습니까? – Brocco

+0

파일을 축소하지 않습니다. – CodeMilian

0

나는 그것을 알아 냈다. 문제는 실제로 내 HTML에서 비롯된 것입니다. 일단 ng-app = "app"을 추가하면 컨트롤러를 참조하는 곳이 모두 작동합니다.

<body ng-controller="UserController" ng-app="app"> 
관련 문제