AngularJS使用指令动态添加和删除元素

AngularJS使用指令动态添加和删除元素,第1张

AngularJS使用指令动态添加和删除元素

我正在解决你的问题。您的问题是指令

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>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5622427.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存