In this PLUNK I have a div with an ng-show
element that invokes a show()
function that returns false. Therefore, I expect the div not to be displayed.
Also, see that the show()
function is invoked twice (the console.log shows the sh
variable twice). How to fix this?
Javascript
var app = angular.module('app', []);
app.directive('mydir', function ($compile) {
var directive = {};
directive.restrict = 'EA';
directive.scope = {
control: '='
};
directive.template = '<div id="root"></div>';
directive.link = function (scope, element, attrs) {
var sh = false;
var wrap = angular.element('<div id="wrap" ng-show="show()"></div>');
wrap.attr('sh', sh);
var wrapc = $compile(wrap)(scope)
element.append(wrapc);
scope.show = function() {
var elem = angular.element( document.querySelector( '#wrap' ) );
var sh = elem.attr('sh');
console.log(sh); // <-- should log false only once, not twice
return sh;
}
};
return directive;
});
HTML
<mydir></mydir>