我想在角度为字符串的父范围内设置事件的名称(单击或触摸)。在子范围上,我想使用该字符串将事件与element.on绑定。
示例:
angular.module('directives', [])
.directive('Sidebar', function () {
'use strict';
return {
link: function (scope) {
//determines whether or not to use click or touch events
//I want to use this to bind events with
//element.on(scope.sidebarClickEvent) in child scopes
scope.sidebarClickEvent = 'click';
},
restrict: 'E',
templateUrl: 'sidebar.html'
};
})
.directive('collapseBtn', function() {
'use strict';
return function (scope, element) {
element.on(scope.sidebarClickEvent /* undefined */ , function () {
//scope.sidebarClickEvent is available here, when the event handler executes
scope.toggleSidebarCollapseState();
element.find('i').toggleClass('icon-double-angle-right');
});
};
})
问题是在绑定事件时,父作用域上定义的属性不可用,因此在绑定事件时scope.sidebarClickEvent是未定义的。但是,如果我将其更改为常规点击事件,那么我可以在事件处理程序中获取该属性。
我是否可以在发生事件绑定时访问作用域继承的属性?我甚至不确定我是否在这里正确理解范围继承,因此在理解中指出错误也将受到赞赏。
感谢。