AngularJS實(shí)現(xiàn)動(dòng)態(tài)編譯添加到dom中的方法
本文實(shí)例講述了AngularJS實(shí)現(xiàn)動(dòng)態(tài)編譯添加到dom中的方法。分享給大家供大家參考,具體如下:
在使用angularjs 時(shí),希望通過動(dòng)態(tài)構(gòu)建angular模版,再通過angular進(jìn)行展示。
使用 方法如下:
<html ng-app="app">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="assets/angular.min.js"></script>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/handlebars.min.js"></script>
<script src="assets/Handlebars.helper.js"></script>
<script >
var app=angular.module("app",[]);
app.controller('ctrl', ['$scope','$compile',function($scope,$compile){
$scope.userName='RAY';
$scope.test = function test(){
console.log('你好:' +$scope.userName);
}
//通過$compile動(dòng)態(tài)編譯html
var html="<button ng-click='test()'>{{userName}}</button>";
var template = angular.element(html);
var mobileDialogElement = $compile(template)($scope);
angular.element(document.body).append(mobileDialogElement);
}]);
</script>
</head>
<body ng-controller="ctrl">
</body>
</html>
var html="<button ng-click='test()'>{{userName}}</button>";
這種代碼就是angular模版,通過angularjs的編譯器進(jìn)行編譯,就能夠訪問到angular scope中的對(duì)象數(shù)據(jù)。
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
Angular.js中ng-include用法及多標(biāo)簽頁面的實(shí)現(xiàn)方式詳解
這篇文章主要給大家介紹了在Angular.js中ng-include用法及多標(biāo)簽頁面的實(shí)現(xiàn)方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-05-05
Ionic+AngularJS實(shí)現(xiàn)登錄和注冊(cè)帶驗(yàn)證功能
這篇文章主要介紹了Ionic+AngularJS實(shí)現(xiàn)登錄和注冊(cè)帶驗(yàn)證功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
如何利用AngularJS打造一款簡單Web應(yīng)用
如果大家希望在應(yīng)用程序的創(chuàng)建工作中采取各類最佳實(shí)踐,那么AngularJS也能夠帶來極大的助益??偠灾?,這套框架的強(qiáng)大功能與特性永遠(yuǎn)不會(huì)讓有著應(yīng)用開發(fā)需求的朋友們失望2015-12-12
詳談angularjs中路由頁面強(qiáng)制更新的問題
下面小編就為大家?guī)硪黄斦刟ngularjs中路由頁面強(qiáng)制更新的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

