微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – Angular嵌套指令不在模型中显示新实体

开发一个角度应用程序,其中包含一个构建目录/嵌套树结构的功能……

我遇到的问题是节点的渲染并没有按预期工作.

只有当列表中已有产品节点并且可以创建部分但是尝试将子部分添加到已添加的部分时,才会呈现产品.部分和产品节点按预期插入到模型中 – 只是指令似乎不在原始模型中不存在的节点上运行.

相关代码

HTML

<head>
    <Meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js" data-semver="1.3.7"></script>
    <script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
    <h1>Menu</h1>
    <button ng-click="addSection()">Add</button>
    <admin-sections sections="menu.sections"></admin-sections>
</body>

</html>

JS

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

app.controller('MainCtrl',function($scope) {
  $scope.menu = {
    sections: [{
      name: "NEW SECTION 1",sections: [{
        name: "NEW SECTION",sections: [],products: [{
          "name": "Product","price": "0.00"
        }]
      }],products: []
    }]
  };

  $scope.addSection = function() {
    $scope.menu.sections.push({
      name: "NEW SECTION",products: []
    });
  };
});

app
  .directive('adminSections',function() {
    return {
      restrict: "E",replace: true,scope: {
        sections: '='
      },templateUrl: 'sections.html'
    };
  })
  .directive('adminSection',function($compile) {
    return {
      restrict: "E",scope: {
        section: '='
      },templateUrl: 'section.html',link: function(scope,element,attrs,controller) {
        if (angular.isArray(scope.section.sections) && scope.section.sections.length > 0) {
          element.append($compile('<admin-sections sections="section.sections"></admin-sections>')(scope));
        }
        if (angular.isArray(scope.section.products) && scope.section.products.length > 0) {
          element.append($compile('<admin-products products="section.products"></admin-products>')(scope));
        }

        scope.addSub = function(section) {
          section.sections.push({
            "name": "NEW SECTION","sections": [],"products": []
          });
        };

        scope.addProduct = function(section) {
          section.products.push({
            "name": "Product","price": "0.00"
          });
        };

        scope.deleteSection = function(section) {
          var idx = scope.$parent.sections.indexOf(section);
          scope.$parent.sections.splice(idx,1);
        };
      }
    };
  })
  .directive('adminProducts',scope: {
        products: '='
      },templateUrl: 'products.html',controller) {
        scope.editProduct = function(product) {
          if (product.price === undefined) {
            product.price = 0;
          }
          element.append($compile('<productform product="product"></productform>')(scope));
        };

        scope.deleteProduct = function(idx) {
          if (confirm('Are you sure you want to delete this product?\n\nClick OK to confirm.')) {
            scope.products.splice(idx,1);
          }
        };
      }
    };
  })
  .directive('adminProduct',scope: {
        product: '='
      },templateUrl: 'product.html',attr,controller) {

        scope.editProduct = function(product) {
          if (product.price === undefined) {
            product.price = 0;
          }
          element.append($compile('<productform product="product" />')(scope));
        };

        scope.deleteProduct = function(idx) {
          scope.$parent.deleteProduct(idx);
        };
      }
    };
  })
  .directive('productform',scope: {
        product: "="
      },templateUrl: 'productform.html',controller) {
        scope.orig = angular.copy(scope.product);
        scope.ok = function() {
          element.remove();
          scope.$parent.editMode = false;
        };

        scope.cancel = function() {
          scope.reset();
          element.remove();
          scope.$parent.editMode = false;
        }

        scope.reset = function() {
          scope.product = angular.copy(scope.orig);
        }
      }
    };
  });

Plunker在这里Angular Tree Menu

希望你能看到意图.

问题是你在链接指令时添加列表,这取决于调用链接函数时的部分状态(只有一次,当有角度看到它时).

当你添加一个新的子节时,它是链接的,但它的子节列表是空的,所以它没有,并且结果元素没有子节,因为你根据调用链接函数时的子节数组状态添加admin-sections,所以根本不会添加嵌套指令.

简单地删除if语句就足够了(或只是检查它们是否是数组):

element.append($compile('<admin-sections sections="section.sections"></admin-sections>')(scope));

element.append($compile('<admin-products products="section.products"></admin-products>')(scope));

这样,指令中的ng-repeat将观察每个部分中的部分数组并相应地更新列表,而当数组为空时保持为空.

Working Plunker

至于嵌套指令是如何工作的,当嵌套指令的链接和控制器函数调用时,这是一个很好的article.

通常,控制器在解析任何内部指令之前运行,并且之后运行链接.所以如果你有这样的嵌套指令:

<outer-directive>
    <inner-directive></inner-directive>
</outer-directive>

订单会是这样的:

>外指令控制器
>内部指令控制器
>内部指令链接
>外部指令链接

这就是为什么当我尝试将admin-sections指令添加到每个部分的模板时,解析器进入无限循环.解析每个部分意味着调用该部分的子部分的另一个链接,但在外部管理部分的链接函数中使用$compile意味着它将在处理外部指令之后进行解析.

此外,内部指令可以要求(docs)父指令使用其控制器.

原文地址:https://www.jb51.cc/angularjs/141060.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐