如何确定是否存在transclude指令中的内容

时间:2014-08-07 03:41:10

标签: angularjs angularjs-directive

假设您有如下指令。

指令

module.directive('person', function() {
  return {
    restrict: 'E',
    scope: {
      header: '='
    },
    transclude:true,
    template: '<div ng-transclude></div>'

  };
});

查看

  <person>
    .....
  </person>

我想知道是否有办法知道<person>中存在被盗的html内容。

感谢。

1 个答案:

答案 0 :(得分:1)

使用link function in指令查找子节点,如下所示

link: function(scope, element, attributes)
      {
            var count = element.find('div')[0].children.length;
            var content = element.find('div')[0];
            console.log(content);
            console.log("Transcluded html content nodes:"+count);
      }

<强> Working Demo

<强> HTML

<div ng-app='myApp' ng-controller="Controller">
    <person>
        <input type="text" ng-model="user" class="ng-scope ng-pristine ng-valid"/>
        <input type="text" ng-model="place" class="ng-scope ng-pristine ng-valid"/>
    </person>
</div>

<强>脚本

var app = angular.module('myApp', []);
app.directive('person', function() {
  return {
    restrict: 'E',
    scope: {
      header: '='
    },
    transclude:true,
    template: '<div ng-transclude></div>',
    link: function(scope, element, attributes){
        var count = element.find('div')[0].children.length;
        var content = element.find('div')[0];
        console.log(content);
        console.log("Transcluded html content nodes:"+count);
    }
  };
});

app.controller('Controller', function ($scope) {

});

<强>输出

enter image description here