相关疑难解决方法(0)

从AngularJS指令访问属性

我的AngularJS模板包含一些自定义HTML语法,如:

<su-label tooltip="{{field.su_documentation}}">{{field.su_name}}</su-label>
Run Code Online (Sandbox Code Playgroud)

我创建了一个指令来处理它:

.directive('suLabel', function() {
  return {
    restrict: 'E',
    replace: true,
    transclude: true,
    scope: {
      title: '@tooltip'
    },
    template: '<label><a href="#" rel="tooltip" title="{{title}}" data-placement="right" ng-transclude></a></label>',
    link: function(scope, element, attrs) {
      if (attrs.tooltip) {
        element.addClass('tooltip-title');
      }
    },
  }
})
Run Code Online (Sandbox Code Playgroud)

一切正常,除了attrs.tooltip表达式,它总是返回undefined,即使该tooltip属性在进行操作时可以从谷歌Chrome的JavaScript控制台中看到console.log(attrs).

有什么建议吗?

更新:Artem提供了一个解决方案.它包括这样做:

link: function(scope, element, attrs) {
  attrs.$observe('tooltip', function(value) {
    if (value) {
      element.addClass('tooltip-title');
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

AngularJS + stackoverflow =幸福

angularjs angularjs-directive

94
推荐指数
2
解决办法
12万
查看次数

标签 统计

angularjs ×1

angularjs-directive ×1