別のJSファイルの角度指令は?
-
21-12-2019 - |
質問
このNGFocusedディレクティブが機能していますが、それを独自のJSファイルに入れたいです。どのように私はその上に配線しますか?
var appObject = angular
.module('app', ['ngAnimate'])
.directive('ngFocused', [
function() {
var FOCUS_CLASS = "focused";
return {
restrict: 'A', //Attribute only
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
ctrl.$focused = false;
element.bind('focus', function(evt) {
element.addClass(FOCUS_CLASS);
scope.$apply(function() { ctrl.$focused = true; });
}).bind('blur', function(evt) {
element.removeClass(FOCUS_CLASS);
scope.$apply(function() { ctrl.$focused = false; });
});
}
}
}
]);
. 解決
私はIIFEアプローチが好きです。あなたの新しいファイルで:
(function(app) {
app.directive(....)
}(angular.module('app'));
.
それはすべてのオブジェクトをグローバルネームスペースから外します。それを参照する前に、角度アプリを定義するスクリプトを必ず参照してください。
他のヒント
それらを2つのファイルに分割するだけです。(<script>
タグを使用して新しいファイルをHTMLファイルに参照することを忘れないでください):
// file app.js
var appObject = angular.module('app', ['ngAnimate'])
// file ngFocused.js
appObject.directive('ngFocused', [ function() {
var FOCUS_CLASS = "focused";
return {
restrict: 'A', //Attribute only
require: 'ngModel',
link: function(scope, element, attrs, ctrl) {
ctrl.$focused = false;
element.bind('focus', function(evt) {
element.addClass(FOCUS_CLASS);
scope.$apply(function() { ctrl.$focused = true; });
}).bind('blur', function(evt) {
element.removeClass(FOCUS_CLASS);
scope.$apply(function() { ctrl.$focused = false; });
})
};
}]);
. 所属していません StackOverflow