DOM操作:你需要的是一個指令!
var elemstyle = angular.module('elemstyle', []);
elemstyle.controller('HeightBasedElCtrl', ['$scope', function($scope){
$scope.data = {
maxH : 200
};
}]);
elemstyle.directive('heightBasedCss', [function(){
return {
transclude : true,
template : '<div ng-class="{true : \'panel panel-success\', false: \'panel panel-danger\'} [ checkHeight() ]" ng-transclude><div>',
link: function(scope, elem, attrs) {
scope.checkHeight = function() {
return elem[0].offsetHeight < scope.data.maxH;
};
}
};
}]);
現在所有你需要做的就是使用指令:
<div height-based-css>
<!--
add whatever content you like here ...
-->
</div>
的jsfiddle嘗試:http://jsfiddle.net/jfabfab/cL3Lda69/
茉莉測試:http://jsfiddle.net/jfabfab/taakfhhs/
注意有關的jsfiddle版:我用結合在textarea上實時查看樣式變化+在茉莉花上的TODO(測試元素offsetHeight變化)
希望它有幫助。
'[parseInt(height)<200]'會起作用嗎? – 2014-11-07 02:52:43