简体   繁体   English

AngularJS ng-click()无法通过控制器中的.append('')函数进行动态调用

[英]AngularJS ng-click() is not working in dynamic call by .append(' ') function in controller

enter image description here I am trying to append HTML elements dynamically which have AngularJS ng-click , but it's not working. 在这里输入图像描述,我试图动态地添加具有AngularJS ng-click HTML元素,但是它不起作用。 I have tried {{2+2}} in iEl.append(<div>{{2+2}}<div>) , but it's not working. 我已经在iEl.append(<div>{{2+2}}<div>)尝试过{{2+2}} ,但是它不起作用。

What am I doing wrong? 我究竟做错了什么?

$scope.appendContent = function() {
    var iEl = angular.element( document.querySelector( '#med2' ) );

    iEl.append('<div class="row well"  ng-controller=Directive id="med1"> <div class="col-sm-12"  > <p>Medication {{2+2}}</p> <div class="col-sm-4"> Medicine <div class="well" dragular="{"classes":{"mirror":"custom-green-mirror"},"nameSpace":"same"}"></div> </div> <div class="col-sm-3"> Frequency <div class="well" dragular="{"classes":{"mirror":"custom-green-mirror"},"nameSpace":"same"}"></div> </div> <div class="col-sm-4"> Days <div class="well" dragular="{"classes":{"mirror":"custom-green-mirror"},"nameSpace":"same"}" ng-click=appendContent()></div> </div> <div class="col-sm-1"> <button class="close cursorDefault" ng-click="removeDIV()">X</button> </div> </div>');
};

I want html view like this, auto append when ever fill the value. 我想要这样的html视图,当填充值时自动追加。

Never manipulate the DOM in Angular if it can be avoided (except if you're in a directive). 如果可以避免,切勿在Angular中操作DOM(除非您在指令中)。 Instead of using querySelector and angular.element , you should use an ng-repeat directive tag inside of your #med2 tag in your HTML (and you could probably remove the id if it isn't used elsewhere). 而不是使用querySelectorangular.element ,您应该在HTML的#med2标签内使用ng-repeat指令标签(如果没有在其他地方使用,则可以删除该ID)。 This ng-repeat tag would include the HTML that's current in your JavaScript string (which should not be in your JavaScript source anyway). ng-repeat标签将包含您的JavaScript字符串中当前包含的HTML(无论如何,该HTML都不应出现在您的JavaScript源中)。 Then, when appendContent is called, instead of appending HTML to the element directly, you should append an object to an array that represents the underlying data controlled by ng-repeat . 然后,当appendContent时,而不是将HTML直接附加到元素,您应该将对象附加到表示ng-repeat控制的基础数据的数组。

use : 采用 :

$scope.appendContent = function() { $ scope.appendContent = function(){

$scope.$apply(function () { $ scope。$ apply(function(){

    var iEl = document.getElementById('med2');
    iEl.append('content') 

}); });

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM