angularjs - 如何在 ui View 中显示自定义指令?

标签 angularjs angularjs-directive angular-ui-router

我的配置如下所示:http://plnkr.co/nya3cr

其中我的状态为:

.state('main.layout', {
        url : '/l',
        abstract : true,
        views : {
          'left' : {
            templateUrl : 'left.html',
            controller : 'LeftCtrl'
          },
          'right' : {
            templateUrl : 'right.html',
            controller : 'RightCtrl'
          },
          'middle' : {
            templateUrl : 'middle.html',
            controller : 'MiddleCtrl'
          },
          'bottom' : {
            templateUrl : 'bottom.html'
          }
        }
      })

我正在尝试在 ui-views 中使用自定义指令,如下所示。

.directive('sidebarDirective', function () {
    return {
      template: 'Hello hello!',
      restrict: 'E',
      link: function postLink(scope, element, attrs) {
        element.text('this is the sidebarDirective directive');
      }
    };
  });

但我无法在 ui-view 元素中使用它,例如在我的配置中,我尝试将自定义指令(侧边栏指令)保留在 left.html

<div sidebar-directive></div>

以及 right.html 中的内容,但它没有显示。我可能缺少一些关于如何在 ui-view 状态中包含指令的关键理解。请帮忙。

最佳答案

对于限制,您应该使用A而不是E。检查更新的version here

  .directive('sidebarDirective', function () {
    return {
      template: 'Hello hello!',
      restrict: 'A', // here should be A, could be even 'AE', but for us A is must!
      link: function postLink(scope, element, attrs) {
        element.text('this is the sidebarDirective directive');
      }
    };
  });

因为 A 表示属性E 表示元素,我们将其用作属性:

<div sidebar-directive></div>

更新版本为here

Developer guide - directive (小引用):

The restrict option is typically set to:

  • 'A' - only matches attribute name
  • 'E' - only matches element name
  • 'C' - only matches class name

These restrictions can all be combined as needed:

  • 'AEC' - matches either attribute or element or class name

关于angularjs - 如何在 ui View 中显示自定义指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099632/

相关文章:

javascript - 将 Node webkit nwdirectory 绑定(bind)到 ngmodel

angularjs - angular-google-maps 通过鼠标点击获取 GEO 位置

javascript - 如何使用 ng-repeat

angularjs - 单独的 Angular 指令

javascript - 路由器 ui "resolve"无法解析 "returned deffered.promise"

angularjs - Angular 在另一个 ui-view 内路由 ui-view

javascript - 如何默认在横向模式下打印

angularjs - 如何使用 ng-transclude 扩展 md-autocomplete? [在模板中非法使用 ngTransclude 指令!]

javascript - 在指令中分配给 $watch 之外的隔离范围对象会产生不可预测的行为

javascript - 在没有 url 帮助的情况下在 Controller 之间传递数据?