AngularJS can automatically initialize the module through ng-app, or manually start the application through angular.bootstrap(document, [module]). No matter which method is used, after the application is started, the dom elements will be dynamically added to the dom tree. , unable to execute angular instructions, that is, unable to bind data and events to dynamically added dom elements through ng-model and ng-click, what should I do?
It is very common to dynamically add DOM elements, such as clicking a button to modify user information on a page, sending an ajax request to query user information, and then using the template engine to compile the static template written in advance on the page into an HTML string. Finally, append the HTML string to the page and display it. Normally we will do this:
<!DOCTYPE html> <html ng-app="app"> <head> <title>demo</title> <meta charset="utf-8"/> <script src="http://cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script> <script id="others_angular_103" type="text/javascript" class="library" src="http://sandbox.runjs.cn/js/sandbox/other/angular.min.js"></script> <style> .contani{ width: 100%; height: 300px; border: 1px solid red; } </style> </head> <body ng-controller="myCtrl"> <script> var app = angular.module('app',[]); app.controller('myCtrl', ['$scope','$compile',function(scope,$compile){ scope.valchange = function(){ console.log('value change') } scope.edit = function(){ //假设这是ajax返回的数据 scope.username = 'wangmeijian'; scope.password = '000000'; $(".contani").html(myTmpl.innerHTML); } }]) </script> <button ng-click="edit()">修改资料</button> <div class="contani"></div> <script type="text/html" id="myTmpl"> <form> 用户名:<input type="text" ng-model="username" /> 密码:<input type="text" ng-model="password" /> </form> </script> </body> </html>
Click the modify data button. The newly inserted dom element is not bound to the data returned by ajax. This is because angular has been initialized before clicking the button. The solution is of course not to initialize it again, but to use $compile to compile the static The HTML of the template is then inserted into the dom tree
<!DOCTYPE html> <html ng-app="app"> <head> <title>demo</title> <meta charset="utf-8"/> <script src="http://cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script> <script id="others_angular_103" type="text/javascript" class="library" src="http://sandbox.runjs.cn/js/sandbox/other/angular.min.js"></script> <style> .contani{ width: 100%; height: 300px; border: 1px solid red; } </style> </head> <body ng-controller="myCtrl"> <script> var app = angular.module('app',[]); app.controller('myCtrl', ['$scope','$compile',function(scope,$compile){ scope.valchange = function(){ console.log('value change') } scope.edit = function(){ //假设这是ajax返回的数据 scope.username = 'wangmeijian'; scope.password = '000000'; //$(".contani").html(myTmpl.innerHTML); $(".contani").append( $compile(myTmpl.innerHTML)(scope) ) } }]) </script> <button ng-click="edit()">修改资料</button> <div class="contani"></div> <script type="text/html" id="myTmpl"> <form> 用户名:<input type="text" ng-model="username" ng-change="valchange()" /> 密码:<input type="text" ng-model="password" ng-change="valchange()" /> </form> </script> </body> </html>
The above is a detailed introduction to AngularJS initialization static template. I hope it will be helpful to everyone's learning.