简体   繁体   English

angularjs $ watch没有在指令中触发

[英]angularjs $watch is not getting triggered in directive

Custom directive $watch is not getting triggered when i use angularjs 1.3 and above. 当我使用angularjs 1.3及更高版本时,不会触发自定义指令$ watch。 it's working with angularjs 1.2. 它与angularjs 1.2一起使用。

Spend many hours, tried almost everything, but it doesn`t work 花了很多时间,几乎尝试了所有方法,但是没有用

code sample : 代码示例:

<ul class="dropdown-list-container" ng-show="isStateOpen" flexcroll="dynamic">
 <li ng-click="selectState(schoolPage.defaultState)">{schoolPage.defaultState.stateName}}</li>
</ul>

$watch is triggered very first time after that it's not triggered newVal and oldVal are undefined. $ watch是在第一次触发之后才触发的,未触发newVal和oldVal未定义。

custom Directives: 定制指令:

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);

        }
    };
})

Try this scope.$parent.$apply() 试试这个scope.$parent.$apply()

running the digest cycle inside might help. 在内部运行摘要循环可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM