Answer the question
In order to leave comments, you need to log in
How to catch false value in directive attribute?
Good afternoon!
When you turn on the history mode in the application, there is a need to check whether everything fit into the screen and if not, correct the current situation, when you exit the mode, return everything to its place
. But I don’t understand why the watcher in the directive catches only true
<div class="history-controls" overflow-header="mode === 'history'" ng-if="mode === 'history'"></div>
define(['app'], function(app) {
app.directive('overflowHeader', function() {
return {
restrict: 'A',
link: function($scope, element, attrs) {
$scope.$watch('overflowHeader', function(e){
console.log(element)
})
$scope.$watch(!('overflowHeader'), function(e){
console.log(element)
})
}
}
});
});
Answer the question
In order to leave comments, you need to log in
It is not correct to pass the !('overflowHeader') construct to $watch . The first parameter must resolve the variable name in the context, and you have written the negation of the string. Accordingly, in the context, he cannot find this.
$scope.$watch('overflowHeader', function(e){
if(e==true){
}
if(e==false){
}
})
Sorry, but this is a complete facepalm.
If you do not match the ngIf condition - there will be no block at all = the directive will not be called.
That is, you will ALWAYS have the value true.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question