angularjs directive call function specified in attribute and pass an argument to it

后端 未结 7 795
难免孤独
难免孤独 2020-12-22 17:11

I want to create a directive that links to an attribute. The attribute specifies the function that should be called on the scope. But I also want to pass an argument to the

7条回答
  •  误落风尘
    2020-12-22 17:46

    My solution:

    1. on polymer raise an event (eg. complete)
    2. define a directive linking the event to control function

    Directive

    /*global define */
    define(['angular', './my-module'], function(angular, directives) {
        'use strict';
        directives.directive('polimerBinding', ['$compile', function($compile) {
    
                return {
                     restrict: 'A',
                    scope: { 
                        method:'&polimerBinding'
                    },
                    link : function(scope, element, attrs) {
                        var el = element[0];
                        var expressionHandler = scope.method();
                        var siemEvent = attrs['polimerEvent'];
                        if (!siemEvent) {
                            siemEvent = 'complete';
                        }
                        el.addEventListener(siemEvent, function (e, options) {
                            expressionHandler(e.detail);
                        })
                    }
                };
            }]);
    });
    

    Polymer component

    
    
    
    
     
    
    
    

    Page

     
    

    searchData is the control function

    $scope.searchData = function(searchObject) {
                        alert('searchData '+ searchObject.text + ' ' + searchObject.range);
    
    }
    

提交回复
热议问题