【问题标题】:Angularjs dynamically adding and removing elements using directiveAngularjs使用指令动态添加和删除元素
【发布时间】:2016-02-16 10:22:51
【问题描述】:

我使用指令来创建联系表格。最初我创建 customerForm 指令来显示客户表格。在该表单中,我有一个按钮,当我们单击添加按钮时,称为 getData 函数,该函数在内部使用 newDirective 来显示 ul 列表。为此,我使用 $compile api 来编译 html 代码。这很好,当我们单击删除按钮时,它还显示列表值和删除按钮,它称为 scope.remove() 函数。但它只删除那些。之后我不能删除任何元素(即按钮)。我不知道为什么会这样。请帮我。 jsfiddle

index.html

<div class="container">
    <customer-form></customer-form>
</div>

app.js

angular.module('sanshaApp', [])

.directive('newDirective', function ( $compile ){
        return {
            restrict: 'E',
            template: 
                '<ul>' +
                    '<li>{{somoe value here}}' +
                        '<button ng-click="remove()">Remove</button>' +
                    '</li>'+
                '</ul>',
            link: function (scope, element, attributes) {
                //called when click on Remove button  
                scope.remove = function () {
                    element.remove();
                }
            }
        }
    })
.directive('customerForm', function($compile) {

        return {

            scope: {

            },

            restrict: 'E',

            transclude: true,

            templateUrl: "../../views/customer-form.html",

            controller: "mainCtrl",

            link: function(scope, element, attrs, mainCtrl) {

                scope.getData = function() {
                   //create new element new-directive
                   $newDirective = angular.element('<new-directive>');
                   element.append($newDirective);
                   //compile html DOM
                   $compile( $newDirective )(scope);
                }
            }
        }
   })

.controller("mainCtrl", ['$scope', function($scope) {

  }])

客户表单.html

   <div class="form-group row">
        <label for="name" class="col-lg-2 form-control-label">Customer name</label>
        <div class="col-lg-4">
            <input type="text" class="form-control" ng-model="name.aa" placeholder="customer name">
        </div>
        <label for="email" class="col-lg-2 form-control-label">Email address</label>
        <div class="col-lg-4">
            <input type="email" class="form-control" ng-model="name.email" placeholder="Email">
        </div>
    </div>
    <div class="form-group row">
        <div class="col-lg-4">
           <button class="btn btn-default" value="add" ng-click="getData()">
               <span class="glyphicon glyphicon-plus"></span>
           </button>
        </div>
     </div>

【问题讨论】:

  • 浏览器 Javascript 控制台出现什么错误?
  • 我在浏览器上没有任何错误,但是 scope.remove() 只被调用一次,之后即使我点击删除按钮也不会被调用。我不知道为什么。 @JohannesJander
  • 在 jsFiddle 中有一个例子很有用。
  • 可能element.remove(); 删除太多,$(element).clear() 可能有效
  • DEMO jsfiddle @StepanKasyanenko

标签: javascript angularjs angularjs-directive angularjs-scope angularjs-ng-repeat


【解决方案1】:

我正在解决你的问题。你的问题是指令 new-directive no 有 isolate 范围。

jsfiddle 上的实时示例。

angular.module('app', [])

  .controller("mainCtrl", ['$scope', function($scope) {

  }])

  .directive('newDirective', function($compile) {
    return {
      restrict: 'E',
      replace: true,
      scope: {},
      template: '<ul>' +
        '<li>' +
        '<button ng-click="remove()">Remove {{ind}}</button>' +
        '</li>' +
        '</ul>',
      link: function(scope, element, attributes) {
        scope.ind = Math.round(Math.random() * 100);
        scope.remove = function() {
          console.log(scope.ind);
          element.remove();
        }
      }
    }
  })

  .directive('customerForm', function($compile) {

    return {

      scope: {},

      restrict: 'E',

      transclude: true,

      template: '<div>' +
        '<input type="button" value="addd" name="AAA" ng-click="getData()">' +
        '</div>',

      controller: "mainCtrl",

      link: function(scope, element, attrs, mainCtrl) {

        scope.getData = function() {
          $newDirective = angular.element('<new-directive>');
          element.append($newDirective);
          $compile($newDirective)(scope);
        }

      }
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <customer-form></customer-form>
</div>

【讨论】:

  • 谢谢@Stepan Kasyanenko
猜你喜欢
  • 2013-10-07
  • 2021-09-26
  • 2019-11-03
  • 2017-07-19
  • 1970-01-01
  • 2019-12-29
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多