在使用angularjs 時(shí),希望通過(guò)動(dòng)態(tài)構(gòu)建angular模版,再通過(guò)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); } //通過(guò)$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模版,通過(guò)angularjs的編譯器進(jìn)行編譯,就能夠訪問(wèn)到angular scope中的對(duì)象數(shù)據(jù)。
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)