How to call a method defined in an AngularJS directive?

前端 未结 13 1295
萌比男神i
萌比男神i 2020-11-22 14:39

I have a directive, here is the code :

.directive(\'map\', function() {
    return {
        restrict: \'E\',
        replace: true,
        template: \'<         


        
13条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 15:31

    If you want to use isolated scopes you can pass a control object using bi-directional binding = of a variable from the controller scope. You can also control also several instances of the same directive on a page with the same control object.

    angular.module('directiveControlDemo', [])
    
    .controller('MainCtrl', function($scope) {
      $scope.focusinControl = {};
    })
    
    .directive('focusin', function factory() {
      return {
        restrict: 'E',
        replace: true,
        template: '
    A:{{internalControl}}
    ', scope: { control: '=' }, link: function(scope, element, attrs) { scope.internalControl = scope.control || {}; scope.internalControl.takenTablets = 0; scope.internalControl.takeTablet = function() { scope.internalControl.takenTablets += 1; } } }; });
    
    

    In controller scope: {{focusinControl}}

    In directive scope:

    Without control object:

提交回复
热议问题