-
Notifications
You must be signed in to change notification settings - Fork 55
/
09-1-compile.html
47 lines (40 loc) · 1.39 KB
/
09-1-compile.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html>
<head>
<title>$compile</title>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
<script>
//controls our form
var app = angular.module('app', []);
app.controller('MyController', function ($compile, $scope) {
$scope.data = {
twoWay : 'hello world'
}
var init = function(){
$(document).ready(function(){
var toInject = '<span ng-bind="data.twoWay">' +
$scope.data.twoWay + '</span><br />' +
'<input type="text" ng-model="data.twoWay" value="' +
$scope.data.twoWay + '">';
$('#uncompiled').html(toInject);
var compiled = $compile(toInject);
$('#compiled').html(compiled($scope));
});
}
init();
});
</script>
<style>div{border:#ccc solid 1px; margin: 15px auto;}</style>
</head>
<body ng-app="app" ng-controller="MyController">
<b>NOTE: Here all three sections must look identical and each should update each other! But notice that the second box doesn't update anything, this is because we did not use $compile on it.</b><br />
Regular bound 2-way connection:
<div>{{data.twoWay}}<br />
<input type="text" ng-model="data.twoWay"></div>
Uncompiled HTML injected after page compiled:
<div id="uncompiled"></div>
$compiled HTML injected after page compiled:
<div id="compiled"></div>
</body>
</html>