-
Notifications
You must be signed in to change notification settings - Fork 55
/
04-1-scope.html
48 lines (47 loc) · 1.1 KB
/
04-1-scope.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
48
<!DOCTYPE html>
<html>
<head>
<title>scope</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.11/angular.min.js"></script>
<script>
var app = angular.module('foodApp',[]);
app.controller('Ctrl', function($scope){
$scope.name="chineese fried rice"
});
//here we show two methods of setting data to an
//isolate scope
app.directive('drink', function(){
return {
restrict:"E",
scope:{},
template:'<div>{{flavor}}</div>',
link:function(scope, elem, attrs){
//gets the flavor='' and sets it to
//the scope
scope.flavor = attrs.flavor;
}
}
});
app.directive('dish', function(){
return {
scope: {
name:"@"
//this is the same as creating a link function above.
},
restrict: 'E',
template: '<div>{{name}}</div>'
};
});
</script>
</head>
<body>
<div ng-app="foodApp" ng-controller='Ctrl'>
<drink flavor="coke"></drink>
<drink flavor="sprite"></drink>
<dish name="butter chicken"></dish>
<dish name="phav baji"></dish>
<!--We see here that we get 'name' from the parent scope-->
<dish name="{{name}}"></dish>
</div>
</body>
</html>