javascript - AngularJS : Customizing the template within a Directive that includes references to scoped attributes

标签 javascript angularjs angularjs-directive

我正在尝试在指令中自定义模板并包含对父范围中的属性的引用。我对 Angular 还很陌生,但我已经做了相当多的搜索,并且我的尝试基于 Customizing the template within a Directive 。但是,如果我将对父作用域变量的引用作为指令的属性传递,则它不会得到解析,可能是因为在调用编译函数时它仍然是未定义的。

我的指令定义如下所示:

app.directive('sectionHeader', function() {
  return {
    restrict: 'EC',
    replace: true,
    transclude: true,
    scope: {sectionName:'@sectionName', imageUrl:'@imageUrl'},
    compile: function(element, attrs) {
      var imageHtml = attrs.hasOwnProperty('imageUrl') ? '<div style="float: left; padding-right: 5px;"><img class="float_left" src="' + attrs.imageUrl + '" alt=""/></div>' : '';
      var htmlText =
        '<div>' + imageHtml + '<h1 class="float-left">' + attrs.sectionName + '</h1>' +
        '<div class="clear"></div>' +
        '<div class="modal_hr"></div></div>';
      element.replaceWith(htmlText);
    },
  };
});

我正在使用这样的指令:

 <div class="section-header" section-name="{{currentFeatureName}}"></div>

问题是在指令上调用编译函数时,我的 Controller 中的 {{currentFeatureName}} 变量似乎没有定义。

我考虑解决这个问题的一种方法是在compile函数中在sectionName属性上设置一个观察者函数,当它看到变化时更新h1元素内容。这看起来有点笨拙,我想知道是否有更好或更优雅的方法来做到这一点。

最佳答案

查看 Directive docs 中的 $observe 函数.

但除此之外,实际上似乎没有必要做你想做的事情。请参阅:

var app = angular.module('plunker', []);
app.controller('AppController',
    [
      '$scope',
      function($scope) {
        $scope.currentFeatureName = 'Current Feature Name';
        $scope.imageUrl = "https://lh3.googleusercontent.com/GYSBZh5RpCFwTU6db0JlHfOr_f-RWvSQwP505d0ZjWfqoovT3SYxIUPOCbUZNhLeN9EDRK3b2g=s128-h128-e365";
      }
    ]
  );

app.directive('sectionHeader', function() {
  return {
    restrict: 'EC',
    replace: true,
    transclude: true,
    scope: {
      sectionName:'@',
      imageUrl:'@'
    },
    template: '<div><div style="float: left; padding-right: 5px;" ng-show="imageUrl"><img class="float_left" ng-src="{{imageUrl}}" alt=""/></div><h1 class="float-left">{{sectionName}}</h1><div class="clear"></div><div class="modal_hr"></div></div>'
  };
});

HTML:

<div ng-controller="AppController">
  <div class="section-header" section-name="{{currentFeatureName}}" image-url="{{imageUrl}}"></div>
</div>

Plunker.

关于javascript - AngularJS : Customizing the template within a Directive that includes references to scoped attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281246/

相关文章:

javascript - 架构错误, 'has no method save'

javascript - 我如何使用 jquery(或只是 javascript)遍历 cookies?

javascript - 尝试构建多个带有引导崩溃的 Accordion 。单击第二个 Accordion 会折叠第一个

javascript - AngularJS 如何实现多态/依赖注入(inject)(最佳实践)

javascript - 如何在 Angular 中访问从 Controller 到自定义指令的范围?

javascript - 以下 angularJS 声明属于哪个命名空间?

javascript - 无法访问资源请求的回调函数内的变量

javascript - HighCharts - 显示 xaxis 标题

javascript - 我收到类型错误 : transclude is not a function and also can't able to bind select options value in select2 in angularjs

angularjs 指令 : $rootScope:infdig error