ng-click attribute on angularjs directive

后端 未结 1 817
时光取名叫无心
时光取名叫无心 2020-12-14 08:51

I think it should be easy to use the well known angular attributes on a directive out of the box.

For example if the name of my directive is myDirective I would like

相关标签:
1条回答
  • 2020-12-14 08:59

    Here is updated code. Maybe is this what you were looking for.

    Html:

    <div data-ng-app="myApp">
        <div data-ng-controller="MyController">
            <my-directive data-ng-click="myFirstFunction('Hallo')"></my-directive>
            <my-directive data-ng-click="mySecondFunction('Hi')"></my-directive>
        </div>
    </div>
    

    Angular:

    var app = angular.module('myApp', []);
    
    app.directive('myDirective', function(){
        return {
            restrict: 'EA',
            replace: true,
            scope: {
                eventHandler: '&ngClick'
            },
            template: '<div id="holder"><button data-ng-click="eventHandler()">Call own function</button></div>'
        };
    });
    
    app.controller('MyController', ['$scope', function($scope) {
        $scope.myFirstFunction = function(msg) {
             alert(msg + '!!! first function call!');   
        };
        $scope.mySecondFunction = function(msg) {
             alert(msg + '!!! second function call!');   
        };
    }]);
    

    Edit

    Check solution that I made in jsFiddler is that what you were looking for?

    http://jsfiddle.net/migontech/3QRDt/1/

    0 讨论(0)
提交回复
热议问题