How to call angular javascript directive methods from the global scope
Posted
by
user1659278
on Stack Overflow
See other posts from Stack Overflow
or by user1659278
Published on 2012-10-06T09:37:11Z
Indexed on
2012/10/14
15:37 UTC
Read the original article
Hit count: 340
JavaScript
|angular
I have a directive like the one of the examples from AngularJS, shown below. How can I call the toggle method from the global scope? I want to be able to toggle the zippy from some legacy code.
myApp.directive('zippy',
function(){
return {
restrict: 'E',
replace: true,
transclude: true,
scope: { title:'bind' },
template:
'<div class="zippy">' +
'<div class="title">{{title}}</div>' +
'<div class="body" ng-transclude></div>' +
'</div>',
link: function(scope, element, attrs) {
var title = angular.element(element.children()[0]),
opened = true;
title.bind('click', toggle);
function toggle() {
opened = !opened;
element.removeClass(opened ? 'closed' : 'opened');
element.addClass(opened ? 'opened' : 'closed');
}
toggle();
}
}
});
© Stack Overflow or respective owner