[javascript] 별도의 AngularJS 컨트롤러 파일을 만드는 방법은 무엇입니까?

모든 AngularJS 컨트롤러가 하나의 파일 controllers.js에 있습니다. 이 파일은 다음과 같이 구성됩니다.

angular.module('myApp.controllers', [])
  .controller('Ctrl1', ['$scope', '$http', function($scope, $http) {
  }])
  .controller('Ctrl2', ['$scope', '$http', function($scope, $http) }
  }])

내가하고 싶은 것은 Ctrl1과 Ctrl2를 별도의 파일에 넣는 것입니다. 그런 다음 index.html에 두 파일을 모두 포함 시키지만 어떻게 구성해야합니까? 나는 이와 같은 일을 시도했지만 웹 브라우저 콘솔에서 컨트롤러를 찾을 수 없다는 오류가 발생합니다. 힌트가 있습니까?

StackOverflow를 검색하고 비슷한 질문을 찾았지만이 구문은 Angular 위에 다른 프레임 워크 (CoffeeScript)를 사용하므로 따라갈 수 없었습니다.


AngularJS : 여러 파일로 컨트롤러를 만드는 방법



답변

파일 하나 :

angular.module('myApp.controllers', []);

두 번째 파일 :

angular.module('myApp.controllers').controller('Ctrl1', ['$scope', '$http', function($scope, $http){

}]);

파일 3 :

angular.module('myApp.controllers').controller('Ctrl2', ['$scope', '$http', function($scope, $http){

}]);

그 순서대로 포함하십시오. 모듈 선언 자체가 가능하도록 3 개의 파일을 권장합니다.


폴더 구조에 관해서는 주제에 대한 많은 의견이 있지만이 두 가지는 꽤 좋습니다.

https://github.com/angular/angular-seed

http://briantford.com/blog/huuuuuge-angular-apps.html


답변

끝에 배열과 함께 angular.module API 사용하면 angular에 새 모듈을 만들도록 지시합니다.

myApp.js

// It is like saying "create a new module"
angular.module('myApp.controllers', []); // Notice the empty array at the end here

배열없이 그것을 사용하는 것은 실제로 게터 함수입니다. 따라서 컨트롤러를 분리하려면 다음을 수행하십시오.

Ctrl1.js

// It is just like saying "get this module and create a controller"
angular.module('myApp.controllers').controller('Ctrlr1', ['$scope', '$http', function($scope, $http) {}]);

Ctrl2.js

angular.module('myApp.controllers').controller('Ctrlr2', ['$scope', '$http', function($scope, $http) {}]);

자바 스크립트를 가져 오는 동안 myApp.js 가 AngularJS 이후이지만 컨트롤러 / 서비스 / 등 앞에 있어야합니다. 그렇지 않으면 angular는 컨트롤러를 초기화 할 수 없습니다.


답변

두 답변 모두 기술적으로 정확하지만이 답변에 다른 구문 선택을 도입하고 싶습니다. 이 imho는 주입으로 무슨 일이 일어나고 있는지를 더 쉽게 읽을 수있게 해줍니다.

파일 하나

// Create the module that deals with controllers
angular.module('myApp.controllers', []);

파일 2

// Here we get the module we created in file one
angular.module('myApp.controllers')

// We are adding a function called Ctrl1
// to the module we got in the line above
.controller('Ctrl1', Ctrl1);

// Inject my dependencies
Ctrl1.$inject = ['$scope', '$http'];

// Now create our controller function with all necessary logic
function Ctrl1($scope, $http) {
  // Logic here
}

파일 3

// Here we get the module we created in file one
angular.module('myApp.controllers')

// We are adding a function called Ctrl2
// to the module we got in the line above
.controller('Ctrl2', Ctrl2);

// Inject my dependencies
Ctrl2.$inject = ['$scope', '$http'];

// Now create our controller function with all necessary logic
function Ctrl2($scope, $http) {
  // Logic here
}


답변

이 솔루션은 어떻습니까? 파일의 모듈 및 컨트롤러 (페이지 끝) 여러 컨트롤러, 지시문 등에서 작동합니다.

app.js

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

myCtrl.js

app.controller("myCtrl", function($scope) { ..});

html

<script src="app.js"></script>
<script src="myCtrl.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

Google은 또한
상황에 따라 그룹화하는 Angular App Structure에 대한 모범 사례 권장 사항을 보유하고 있습니다 . 하나의 폴더에있는 모든 HTML이 아니라 로그인을위한 모든 파일 (html, css, app.js, controller.js 등). 따라서 모듈에서 작업하면 모든 지시문을 쉽게 찾을 수 있습니다.


답변

간결성을 위해 다음은 전역 변수에 의존하지 않는 ES2015 샘플입니다.

// controllers/example-controller.js

export const ExampleControllerName = "ExampleController"
export const ExampleController = ($scope) => {
  // something... 
}

// controllers/another-controller.js

export const AnotherControllerName = "AnotherController"
export const AnotherController = ($scope) => {
  // functionality... 
}

// app.js

import angular from "angular";

import {
  ExampleControllerName,
  ExampleController
} = "./controllers/example-controller";

import {
  AnotherControllerName,
  AnotherController
} = "./controllers/another-controller";

angular.module("myApp", [/* deps */])
  .controller(ExampleControllerName, ExampleController)
  .controller(AnotherControllerName, AnotherController)


답변

그렇게 우아하지는 않지만 전역 변수를 사용하는 구현 솔루션에서 매우 간단합니다.

“첫 번째”파일에서 :


window.myApp = angular.module("myApp", [])
....

“second”, “third”등에서


myApp.controller('MyController', function($scope) {
    ....
    }); 


답변