AngularJS初始化靜態(tài)模板詳解
AngularJS可以通過ng-app來自動初始化模塊,也可以通過angular.bootstrap(document, [module])手動啟動應(yīng)用,不管用哪種方法,應(yīng)用啟動后,動態(tài)往dom樹里面添加的dom元素,無法執(zhí)行angular指令,即無法通過ng-model、ng-click給動態(tài)添加的dom元素綁定數(shù)據(jù)和事件,怎么辦?
動態(tài)添加dom元素的場景非常常見,如點擊某頁面上修改用戶資料的按鈕,發(fā)送ajax請求去查詢用戶資料,然后通過模板引擎將事先寫在頁面里的靜態(tài)模板編譯成HTML字符串,最后將HTML字符串a(chǎn)ppend到頁面顯示出來,一般情況下我們會這樣做:
<!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(){
//假設(shè)這是ajax返回的數(shù)據(jù)
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>
點擊修改資料按鈕,新插入的dom元素并沒有綁定ajax返回的數(shù)據(jù),這是因為點擊按鈕前angular已經(jīng)初始化完畢了,解決辦法當然不是再初始化一次,而是單獨使用$compile編譯靜態(tài)模板的HTML,然后再插入dom樹中
<!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(){
//假設(shè)這是ajax返回的數(shù)據(jù)
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>
以上就是關(guān)于AngularJS初始化靜態(tài)模板的詳細介紹,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
AngularJs ng-repeat 嵌套如何獲取外層$index
這篇文章主要介紹了AngularJs ng-repeat 嵌套如何獲取外層$index的相關(guān)資料,需要的朋友可以參考下2016-09-09
AngularJS的依賴注入實例分析(使用module和injector)
這篇文章主要介紹了AngularJS的依賴注入,結(jié)合實例形式分析了依賴注入的原理及使用module和injector實現(xiàn)依賴注入的步驟與操作技巧,需要的朋友可以參考下2017-01-01
AngularJs bootstrap搭載前臺框架——基礎(chǔ)頁面
本文主要介紹AngularJs bootstrap搭載前臺框架基礎(chǔ)頁面的建設(shè),這里整理餓了相關(guān)資料及實現(xiàn)實例代碼,有興趣的小伙伴可以參考下2016-09-09

