사용자 지정 서비스를 삽입하지 않았는데도 아래 코드가 작동하는 이유가 혼란 스럽습니다. 나는 당신이 주사해야 할 때와 주사하지 않을 때 얻지 못합니다.
http://plnkr.co/edit/aCpIYleNL4p9IRA0aAzr?p=preview
(function() {
'use strict';
angular.module('svccApp', []);
angular.module('svccApp').
factory('bareService', [
function() {
var myValue = {};
myValue.str1 = 'xyz';
return myValue;
}
]);
angular.module('svccApp')
.controller('MyController', MyController);
// WHY IS THIS NOT NECESSARY?
//MyController.$inject = ['$scope', 'bareService'];
function MyController($scope, bareService) {
$scope.testVal = bareService.str1;
}
}());
Angular는 컨트롤러의 매개 변수 이름을보고 무엇을 주입해야하는지 파악할 수있을만큼 똑똑합니다. 그러나 MyController.$inject = ['$scope', 'bareService'];
매개 변수 이름이 엉망이되기 때문에 코드를 축소하는 경우 문자열 (예 :) 로 삽입하려는 항목을 수동으로 지정해야합니다 .
예를 들면 다음과 같습니다.
function MyController($scope, bareService) {
$scope.testVal = bareService.str1;
}
다음과 같이 축소 될 수 있습니다.
function MyController(a, b) {
a.testVal = b.str1;
}
의존성 주입이 중단됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다