我目前正在为幻灯片菜单指令开发一个AngularJS。javascript包含三种类型的指令:针对每种类型的滑动菜单的指令(为了简洁起见,我只包含左滑动菜单)、屏幕其余部分的包装指令asmWrapper和控制按钮指令asmControl。目前,所有这些指令都在使用服务asmService进行通信。
当用户单击asmControl时,该指令的控制器在asmService上调用一个方法,该方法确定哪个菜单已被触发,并在$rootScope上发出一个“asmEvent”。asmSlidingMenu's控制器将捕获该事件并更新其作用域中的活动变量,但DOM元素的CSS类保持不变。
我假设没有设置ng类。我该怎么解决这个问题?
我已经包含了下面asmSlidingMenu指令的代码。要查看更完整的示例,请查看我创建的柱塞。
slideMenu.directive('asmSlideLeft', ['$rootScope', 'asmService',
function($rootScope, asmService) {
return {
restrict: 'AEC'
, scope: {}
, controller: function($scope) {
$rootScope.$on('asmEvent', function(event, prop) {
console.log('Intercepted: ' + asmService.asmStates.slideLeft.active);
$scope.active = asmService.asmStates.slideLeft.active;
});
}
, compile: function(element, attrs) {
attrs.$set('class', 'asm asm-horizontal asm-left');
attrs.$set('data-ng-class', '{"asm-left-open: active"}');
return {
pre: function preLink(scope, iElement, iAttrs) {}
, post: function postLink(scope, iElement, iAttrs) {}
}
}
}
}]);发布于 2014-04-26 07:12:35
首先,active位于隔离作用域中,因此ng-class无法访问它。
其次,更重要的是,ng-class是在之后添加的,元素的指令是通过角度收集的。太晚了。
如果您有自己的指令,就没有理由使用ng-class。
slideMenu.directive('asmSlideLeft', ['$rootScope', 'asmService',
function($rootScope, asmService) {
return {
restrict: 'AEC'
...
link: function(scope, element) {
element.addClass('asm asm-horizontal asm-left');
$rootScope.$on('asmEvent', function() {
if (asmService.asmStates.slideLeft.active) {
element.addClass('asm-left-open');
}
else {
element.removeClass('asm-left-open');
}
...https://stackoverflow.com/questions/23304673
复制相似问题