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

