当我使用angularjs 1.3及更高版本时,不会触发自定义指令$ watch。它正在与angularjs 1.2一起使用。
花了很多时间,几乎尝试了所有方法,但是没有用
代码示例:
<ul class="dropdown-list-container" ng-show="isStateOpen" flexcroll="dynamic">
<li ng-click="selectState(schoolPage.defaultState)">{schoolPage.defaultState.stateName}}</li>
</ul>
在未触发$ watch之后第一次触发$ watch,newVal和oldVal未定义。
自定义指令:
directive('flexcroll', function ($timeout) {
return {
restrict: 'A',
link: function (scope, lElement, attr) {
var parentHeight = lElement.height();
var child = lElement.children(':eq(0)');
scope.childHeight= function(){
return child.height();
};
scope.$watch(scope.childHeight, function(newVal, oldVal){
if(attr.flexcroll=='dynamic'){
var maxHeight = parseInt(lElement.css('max-height'));
var childNewHeight = newVal;
if(childNewHeight > maxHeight){
lElement.css({'height': maxHeight+"px"});
} else {
lElement.css({'height': (childNewHeight+5)+"px"});
}
}
},true);
}
};
})
答案 0 :(得分:0)
尝试一下scope.$parent.$apply()
在内部运行摘要循环可能会有所帮助。