我有一个指令,类似于 AngularJS 的示例之一,如下所示。如何从全局范围调用切换方法?我希望能够从一些遗留代码中切换 zippy。
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();
}
}
});