如何在angularjs中的父元素上添加单击操作

时间:2016-06-13 06:54:05

标签: angularjs angularjs-directive element parent

我有一个指令

<child-div child-data="childData"></child-div>

我要求我必须根据以下情况关联点击操作

  1. 如果childData.view == true,则在指令
  2. 上关联点击操作
  3. 如果childData.view == false,则在其父级上关联操作。
  4. 问题是当条件2满足时,我无法获得childDiv指令的父元素。

    任何人都可以告诉我我该怎么做。 在此先感谢

    'use strict';
    
    angular.module('testDirectives')
    .directive('childDiv', function ( $compile, $q){
    return {
        restrict: 'EAC',
        scope: {
            actionInstance: '=',
        },
        replace : true,
        templateUrl : 'template.html',
        link: function (scope, element, attrs) {
            status = isViewAssociatedWithAction();
            if(status){ //action on directive
    
                element.css('cursor', 'pointer');
                element.bind('click', function(){
                    actionCode = scope.actionInstance.actionCode;
                    //do something
                })
            }
            else{//action on parent element of directive need to apply
    
                element.parent().on('click', function () {
                    console.log('Parent div has action');
                })
            }
            function isViewAssociatedWithAction(){
                return (scope.actionInstance.showActionLabel || scope.actionInstance.showActionIcon) ? true : false;
            }
        }
    }
    

    });

0 个答案:

没有答案