60

ディレクティブの UI の変更を「監視」することはできますか? そんな感じ:

.directive('vValidation', function() {
    return function(scope, element, attrs) {
        element.$watch(function() {
            if (this.hasClass('someClass')) console.log('someClass added');
        });
    }
})
4

3 に答える 3

40
attrs.$observe('class', function(val){});
于 2013-04-09T19:59:31.933 に答える
2

コントローラーで変数を監視することもできます。

このコードは、他のモジュールがフィードバック メッセージを表示した後、通知バーを自動的に非表示にします。

HTML:

<notification-bar
    data-showbar='vm.notification.show'>
        <p> {{ vm.notification.message }} </p>
</notification-bar>

指令:

var directive = {
    restrict: 'E',
    replace: true,
    transclude: true,
    scope: {
        showbar: '=showbar',
    },
    templateUrl: '/app/views/partials/notification.html',
    controller: function ($scope, $element, $attrs) {

        $scope.$watch('showbar', function (newValue, oldValue) {
            //console.log('showbar changed:', newValue);
            hide_element();
        }, true);

        function hide_element() {
            $timeout(function () {
                $scope.showbar = false;
            }, 3000);
        }
    }
};

指令テンプレート:

<div class="notification-bar" data-ng-show="showbar"><div>
    <div class="menucloud-notification-content"></div>
于 2016-09-15T11:34:53.527 に答える