Creating single custom directive for ng-click to perform multiple controller function

亡梦爱人 提交于 2020-01-14 06:21:20

问题


I have the following code:

<div>...
    <div class="glyphicon glyphicon-filter ng-click="vm.add()" tabindex="1">
    <a href='' tabindex="2"><img id="touch" ng-click="vm.multiply(xyz)" 
    src="/ui/assets/images/xxx.png"/></a> 
    <div class="glyphicon glyphicon-filter"ng-click="vm.showId()" tabindex="1" title="Filter">
    </div>
..</div>  

I want to create a custom single ng-click directive as its recommended for div (ng-click to be used for only buttons). I want to know if there is any way I can create a single directive for all 3 ng-click and call those 3 different functions in link at $apply?


回答1:


Here you go: http://jsfiddle.net/psevypcs/2/

HTML

<div clicky="test()">test</div>
<div clicky="test2()">test2</div>

AngularJS-Controller

$scope.test = function(){
       alert('hy');
   };


$scope.test2 = function(){
       alert('hy2');
   };

AngularJS-Directive

angular.module('myApp')
        .directive('clicky', Clicky);

    function Clicky() {
        return {
            restrict: 'A',
            scope: {
                clicky: '&'  // Take yourself as variable
            },
            link: function(scope, element, attrs){
               $(element).on('click', function(e) {
                  scope.clicky();
               });
            }
      };
    }


来源:https://stackoverflow.com/questions/35570189/creating-single-custom-directive-for-ng-click-to-perform-multiple-controller-fun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!