【问题标题】:Using the AngularJS require option to call into another directive使用 AngularJS 的 require 选项调用另一个指令
【发布时间】:2015-01-09 15:11:36
【问题描述】:

我只是在这里阅读有关通过 require 选项从另一个指令中访问一个指令的控制器的信息:

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

在我看来,指令 droppable 和仪表板声明 - 在两个不同的 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。

在我的 droppable 指令中,我使用 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。请帮我弄清楚如何使用require。

我实际上需要调用链接选项中的 addWidget() 函数;但我想我可以将其复制或移动到控制器选项中。

谢谢! 鲍勃

【问题讨论】:

    标签: javascript html angularjs angularjs-directive


    【解决方案1】:

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

    Here是一篇看指令对指令通信的好文章

    小提琴示例也是根据您之前的问题构建的

    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);
               }
            }
        }
    }]);
    

    Updated JSFiddle

    【讨论】:

    • 有趣的事情:如果我将它从 require: '?dashboard' 更改为 `require: 'dashboard'',我会收到一个 Angular 未定义错误。似乎在编译 'droppable' 时,'dashboard' 指令尚不可用。至少这是我的理论,考虑到'?表示它是可选的。
    • 你在搞定它吗? require: 'dashboard'require: '?dashboard' 似乎都很好。会不会是你那边的其他东西?
    • 使用'?dashboard' 在你的小提琴中工作得很好。可能是因为我的两个指令是从单独的 js 文件加载的吗?我先加载了仪表板指令文件,但也许我需要合并它们。
    • 我不确定,但我希望这不是问题。您是否在同一元素上有指令声明,例如&lt;button id="dash" dashboard draggable&gt;&lt;/button&gt;?这个答案是否帮助您找到正确的方向?
    • 我很抱歉,因为我从来没有真正能够解决这个问题。从好的方面来说,您对 stackoverflow.com/questions/27848095/… 的帮助将我推向了正确的方向。
    猜你喜欢
    • 2014-10-19
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多