尤里卡!
http://jsfiddle.net/G33PE/5/
var leanwxApp = angular.module('LeanwxApp', [], function() {});
var controllers = {};
var directives = {};
directives.insertStyle = [ function(){
return {
link: function(scope, element, attrs) {
scope.theStyle = window.getComputedStyle(element[0].parentElement, null)
}
}
}];
leanwxApp.controller(controllers);
leanwxApp.directive(directives);
使剛剛拍了很多持久性和猜測。也許超時是不必要的,但在調試時,似乎我只在超時發生後才從父級獲取樣式值。
而且我不知道爲什麼,但我不得不去到parentElement獲取其風格(即使它現實地繼承(聳肩)?)
更新小提琴再次
有沒有一個沒有超時,但只是看着風格的parentElement,它似乎仍然工作,所以抓住風格不可用的懷疑,它只是沒有在我所期望的。
而且聖牛有很多的在Chrome瀏覽器來調試: https://developers.google.com/chrome-developer-tools/docs/javascript-debugging
我使用的代碼
debugger;
聲明中斷點下降,而無需搜索所有的小提琴文件。
一個更快更新
下面的代碼從AngularUI隊出來的自舉的UI,並聲稱提供觀看相應的事件的手段(有沒有試過,但它看起來像它應該幫幫我)。
http://angular-ui.github.io/bootstrap/
/**
* $transition service provides a consistent interface to trigger CSS 3 transitions and to be informed when they complete.
* @param {DOMElement} element The DOMElement that will be animated.
* @param {string|object|function} trigger The thing that will cause the transition to start:
* - As a string, it represents the css class to be added to the element.
* - As an object, it represents a hash of style attributes to be applied to the element.
* - As a function, it represents a function to be called that will cause the transition to occur.
* @return {Promise} A promise that is resolved when the transition finishes.
*/
.factory('$transition', ['$q', '$timeout', '$rootScope', function($q, $timeout, $rootScope) {
var $transition = function(element, trigger, options) {
options = options || {};
var deferred = $q.defer();
var endEventName = $transition[options.animation ? "animationEndEventName" : "transitionEndEventName"];
var transitionEndHandler = function(event) {
$rootScope.$apply(function() {
element.unbind(endEventName, transitionEndHandler);
deferred.resolve(element);
});
};
if (endEventName) {
element.bind(endEventName, transitionEndHandler);
}
// Wrap in a timeout to allow the browser time to update the DOM before the transition is to occur
$timeout(function() {
if (angular.isString(trigger)) {
element.addClass(trigger);
} else if (angular.isFunction(trigger)) {
trigger(element);
} else if (angular.isObject(trigger)) {
element.css(trigger);
}
//If browser does not support transitions, instantly resolve
if (!endEventName) {
deferred.resolve(element);
}
});
// Add our custom cancel function to the promise that is returned
// We can call this if we are about to run a new transition, which we know will prevent this transition from ending,
// i.e. it will therefore never raise a transitionEnd event for that transition
deferred.promise.cancel = function() {
if (endEventName) {
element.unbind(endEventName, transitionEndHandler);
}
deferred.reject('Transition cancelled');
};
return deferred.promise;
};
// Work out the name of the transitionEnd event
var transElement = document.createElement('trans');
var transitionEndEventNames = {
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'transitionend',
'OTransition': 'oTransitionEnd',
'transition': 'transitionend'
};
var animationEndEventNames = {
'WebkitTransition': 'webkitAnimationEnd',
'MozTransition': 'animationend',
'OTransition': 'oAnimationEnd',
'transition': 'animationend'
};
function findEndEventName(endEventNames) {
for (var name in endEventNames){
if (transElement.style[name] !== undefined) {
return endEventNames[name];
}
}
}
$transition.transitionEndEventName = findEndEventName(transitionEndEventNames);
$transition.animationEndEventName = findEndEventName(animationEndEventNames);
return $transition;
}]);
不要這樣做(使用控制器)!使用指令! –
所以你的建議只是創建一個插入樣式值的指令。 – Nix
同意該指令。 – rGil