首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我把name作为属性而不是标记,为什么指令不能工作呢?

如果我把name作为属性而不是标记,为什么指令不能工作呢?
EN

Stack Overflow用户
提问于 2014-10-26 14:32:31
回答 2查看 53关注 0票数 0

角度文献的示例中,可以通过将其名称作为<div>中的一个属性来使用它。给出的例子是

代码语言:javascript
复制
<div ng-controller="Controller">
  <div my-customer></div>
</div>

js看起来就像

代码语言:javascript
复制
angular.module('docsSimpleDirective', [])
  .controller('Controller', ['$scope', function($scope) {
    $scope.customer = {
      name: 'Naomi',
      address: '1600 Amphitheatre'
    };
  }])
  .directive('myCustomer', function() {
    return {
      template: 'Name: {{customer.name}} Address: {{customer.address}}'
    };
  });

但是,在类似的示例中,如果我将html代码从<tree>更改为<div tree>,代码将不再工作。

为什么不行?

来自JS Fiddle的代码:

代码语言:javascript
复制
<div ng-app="myapp">
    <div ng-controller="TreeCtrl">
        <tree family="treeFamily">
            <p>{{ family.name }}</p>
        </tree>
    </div>
</div>


var module = angular.module('myapp', []);

module.controller("TreeCtrl", function($scope) {
    $scope.treeFamily = {
        name : "Parent",
        children: [{
            name : "Child1",
            children: [{
                name : "Grandchild1",
                children: []
            },{
                name : "Grandchild2",
                children: []
            },{
                name : "Grandchild3",
                children: []
            }]
        }, {
            name: "Child2",
            children: []
        }]
    };
});

module.directive("tree", function($compile) {
    return {
        restrict: "E",
        transclude: true,
        scope: {family: '='},
            template:       
            '<ul>' + 
                '<li ng-transclude></li>' +
                '<li ng-repeat="child in family.children">' +
                    '<tree family="child">{{family.name}}</tree>' +
                '</li>' +
            '</ul>',
        compile: function(tElement, tAttr, transclude) {
            var contents = tElement.contents().remove();
            var compiledContents;
            return function(scope, iElement, iAttr) {
                if(!compiledContents) {
                    compiledContents = $compile(contents, transclude);
                }
                compiledContents(scope, function(clone, scope) {
                         iElement.append(clone); 
                });
            };
        }
    };
});

tree {
    margin-left: 20px;
    display: block;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-26 14:53:15

限制选项用于指定如何在页面上调用指令。有四种不同的方法来调用指令,因此有四种有效的限制选项:

代码语言:javascript
复制
'A' - attribute - <span ng-sparkline></span>
'E' - element - <ng-sparkline></ng-sparkline>
'C' - class - <span class="ng-sparkline"></span>
'M' - comment - <!-- directive: ng-sparkline -->

在您的例子中,它不起作用,因为它被定义为限制'E‘-元素。

票数 2
EN

Stack Overflow用户

发布于 2014-10-26 14:37:32

这是因为指令中的restrict选项。

在这里,它被设置为e,这意味着只匹配元素名。

更多https://docs.angularjs.org/guide/directive

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26574057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档