<html lang="en" ng-app="MyModule">
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src=".js"></script>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div ng-controller="MyCtrl">
<div class="panel panel-default">
<!-- @ -->
<div class="panel-title">
<drink flavor="{{ctrlFlavor}}"></drink>
<button ng-click="resetDrflavor()" class="btn btn-warnning">resetDrflavor</button>
<input type="text" name="" class="input" ng-model="ctrlFlavor">
</div>
</div>
<div class="panel panel-primary"> <!-- = -->
<div class="panel-title"><input type="text" name="" ng-model="eatFoodS">
<eat eatfooda="eatFoodS"></eat>
</div>
</div>
<div class="panel panel-warning"><!-- & -->
<div class="panel-title">
<play aaa="ggg('嘿哟~')"></play>
</div>
</div>
</div>
</body>
</html>
/*-------------------------------------------------------------------js部分-----------------------------------------------------------------------*/
var myModule=angular.module("MyModule",[]);
myModule.controller("MyCtrl",['$scope',function ($scope) {
$scope.ctrlFlavor="Baiwei";
$scope.eatctrlFlavor="糖醋里脊";
$scope.resetDrflavor=function(){
$scope.ctrlFlavor="百事可乐";
}
$scope.resetEtflavor=function(){
$scope.eatctrlFlavor="脆皮蛋卷";
}
$scope.ggg = function(s) {
alert("我成功绑定了父级作用域中的aaa函数!" + s);
}
}]);
myModule.directive("drink",function(){
return{
restrict:'AE',
scope:{
f1:'@flavor' //存储父元素中的flavor属性的值
},
template:"'new'+<div>{{f1}}</div>",
}
});
myModule.directive("eat",function(){
return{
restrict:'ECMA',
scope:{
eatfood:'=eatfooda' //存储父元素中eatfooda的引用
// 这里有个大坑
// *****如果html属性中名称是驼峰法命名!!!在js里大写变小写!!!
// *****html里是 - (eat-food)写法,则js里是驼峰法(eatFood)
},
template:"<input type='text' ng-model='eatfood'>",
}
});
myModule.directive("play",function(){
return{
restrict:'AE',
scope:{
child:'&aaa' //存储父元素中的方法aaa
},
template:'<input type="button" ng-click="child()" value="点我哟~">',
}
});