がAngularJS by Brad Greenから、このスニペットを考えてみましょう。なぜAngularJSで配列を渡すのですか?
var directives = angular.module('guthub.directives', []);
directives.directive('butterbar', ['$rootScope',
function ($rootScope) {
return {
link: function (scope, element, attrs) {
element.addClass('hide');
$rootScope.$on('$routeChangeStart', function() {
element.removeClass('hide');
});
$rootScope.$on('$routeChangeSuccess', function() {
element.addClass('hide');
});
}
};
}]
);
directives.directive('focus', function() {
return {
link: function (scope, element, attrs) {
element[0].focus();
}
};
});
「butterbar」ディレクティブのために、彼は最初の項目は、依存関係名"$rootScope"
と単なる文字列で、2番目の項目が関数である配列を渡すことに注意してください。この関数は$rootScope
への依存関係を宣言します。なぜここで自分たちを繰り返すのですか?それだけでこれを行うことが可能であるように思われる場合、特に:
directives.directive('butterbar', function ($rootScope) {
return {
link: function (scope, element, attrs) {
element.addClass('hide');
$rootScope.$on('$routeChangeStart', function() {
element.removeClass('hide');
});
$rootScope.$on('$routeChangeSuccess', function() {
element.addClass('hide');
});
}
};
});
私は、文字列であること、依存関係の名前は重要性のいくつかの並べ替えを持っていることを推測しています。誰が私がなぜディレクティブのためだけでなく、本の中でこれを行うのか教えてもらえますか?
縮小VS依存性の注入。 – elclanrs
明快さの4つの単語:) – Chev