コントローラーで変数を監視することもできます。
このコードは、他のモジュールがフィードバック メッセージを表示した後、通知バーを自動的に非表示にします。
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>