使用AngularJS需要选项调用到另一个指令(Using the AngularJS requir

2019-10-21 14:05发布

我只是读关于在这里通过需要选择另一个指令中访问一个指令的控制器:

  http://jasonmore.net/angular-js-directives-difference-controller-link/

该指令可放开和仪表板的声明对我的看法 - 在两个不同的div:

  <div class="wrapper wrapper-content animated fadeInRight">
<div class="row">        
    <div class="col-lg-12" data-droppable drop="handleDrop">
        <div id="dash" dashboard="dashboardOptions" class="dashboard-container"></div>
    </div>
</div>

不过,我似乎无法得到它的工作。 下面我dashboardCtrl参数是NULL。

在这里,我投掷的指令,我使用require选项:

  .directive('droppable', function () {
return {
    scope: {
        drop: '&',
    },
    //****************** dashboard directive is optionally requested ************
    require: '?dashboard', 

    link: function (scope, element, attributes, dashboardCtrl) {
         el.addEventListener('drop', function (e) {

            if (e.preventDefault) { e.preventDefault(); }

            this.classList.remove('over');
            var item = document.getElementById(e.dataTransfer.getData('Text'));                
            this.appendChild(item.cloneNode(true));

            // *** CALL INTO THE dashboardCtrl controller ***
            dashboardCtrl.addWidgetInternal();


            return false;
        }, false);
    }
}
});

和仪表盘指令:

 angular.module('ui.dashboard')
.directive('dashboard', ['WidgetModel', 'WidgetDefCollection', '$modal', 'DashboardState', '$log', function (WidgetModel, WidgetDefCollection, $modal, DashboardState, $log) {
  return {
      restrict: 'A',
      templateUrl: function (element, attr) {
          return attr.templateUrl ? attr.templateUrl : 'app/shared/template/dashboard.html';
      },
      scope: true,      
      controller: ['$scope', '$attrs', function (scope, attrs) {
            // ommitted for brevity
        }],
      link: function (scope) {                
            scope.addWidgetInternal = function (event, widgetDef) {
              event.preventDefault();
              scope.addWidget(widgetDef);
          };
      };
   }
}]);

然而,我的dashboardCtrl参数为NULL。 请帮我弄清楚如何使用要求。

其实,我需要调用addWidget()函数,这是链接选项之内; 但我想我可以复制或移动到控制器选项。

谢谢 ! 短发

Answer 1:

这里是“父”指令的示例dashboard需要droppable ,并利用两者之间的通信require和通过dashboardCtrl

这里是一个很好的文章,看看指令,指令通信

小提琴例子也从以前的问题,建

的jsfiddle

app.directive('droppable', [function () {
    return {
        restrict: 'A',
        require: 'dashboard',
        link: function (scope, elem, attrs, dashboardCtrl) {

            dashboardCtrl.controllerSpecificFunction('hello from child directive!');

            scope.addWidgetInternal = function(message) {
                console.log(message);
            }
        }
    }
}]);

app.directive('dashboard', [function () {
    return {
        restrict: 'A',
        controller: function ($scope) {
            $scope.handleDrop = function(message) {
                $scope.addWidgetInternal(message)
            }

            this.controllerSpecificFunction = function(message) {
                console.log(message);
           }
        }
    }
}]);


编辑

根据讨论,这里是我目前了解的问题要解决

家长指导dashboard可选要求孩子指令droppable并需要有沟通两者之间

<div dashboard>
    <button id="dash" droppable ng-click="handleDrop($event)">Handle Drop</button>
</div>


app.directive('droppable', [function () {
    return {
        restrict: 'A',
        require: '^?dashboard',
        link: function (scope, elem, attrs, dashboardCtrl) {
            scope.handleDrop = function($event) {
                dashboardCtrl.addWidgetInternal($event);
            }
        }
    }
}]);

app.directive('dashboard', [function () {
    return {
        restrict: 'A',
        controller: function ($scope) {
            this.addWidgetInternal = function($event) {
                console.log($event);
           }
        }
    }
}]);

更新的jsfiddle



文章来源: Using the AngularJS require option to call into another directive