隨着Class.js,我可以靈活Ë xtend任何指令。
(function (define) {
"use strict";
define([
'BaseClass/BaseDirective',
'BaseClass/Component'
], function (BaseDirective, Component) {
var AnimateWhenHover = BaseDirective.extend({
$animate: null,
init: function ($animate, scope, element, attr) {
this.$animate = $animate;
this._super(scope, element, attr);
},
defineScope: function() {
var data = this.$scope.$eval(this.$attr.animateWhenHover);
this.$scope.selector = data.show;
this.$scope.overAnimation = data.over || "l2r";
this.$scope.outAnimation = data.out || "l2r";
},
defineListeners: function() {
this.$element.hover(this.in.bind(this), this.out.bind(this));
},
in: function() {
this.$element.css({"width": "auto"});
this.$animate.addClass($(this.$scope.selector), this.$scope.overAnimation);
},
out: function() {
this.$element.css({"width": "50px"});
this.$animate.removeClass($(this.$scope.selector), this.$scope.outAnimation);
}
});
return new Component("animateWhenHover", ["$animate", function ($animate) {
return {
scope: '@',
restrict: 'A',
link: function (scope, element, attr) {
new AnimateWhenHover($animate, scope, element, attr)
}
}
}
]);
});
}
(define));
你應該用ng-init調用一個控制器方法來代替這個workarroud。甚至創建一個特定的指令來解決這個問題。 – Fals
或者我可以從一個指令擴展到使我的代碼更靈活嗎? – tom10271
這不是一個很好的實踐,因爲你正在使用jQuery,即使在控制器內部也是如此。你應該在特定的指令中對待它。而不是傳遞函數,只需傳遞ID或類以set-height-to並在調用css函數的指令中執行作業 – Fals