<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="libs/angular.min.js"></script>
<script>
angular.module("myApp",[])
.service("gaoyn_greeter",function(){ //使用service创建一个服务
this.sayHello=function(){
alert("你好factory");
}
this.saybye=function(){
alert("再见");
}
//可以不用return 对象出去,直接使用Service对象--->自动create一个对象出来
})
//使用 angular.module("myApp",[]).controller
.controller("democ",["$scope","gaoyn_greeter",function($scope,gaoyn_greeter){
$scope.hi=function(){
gaoyn_greeter.sayHello();
}
}])
.controller("demoB",function($scope,gaoyn_greeter){
$scope.hello=function(){
gaoyn_greeter.saybye();
}
})
;
</script>
</head>
<body ng-app="myApp" ng-controller="democ">
<button ng-click="hi()">问好</button>
<button ng-controller="demoB" ng-click="hello()">再见</button>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="libs/angular.min.js"></script>
<script>
angular.module("myApp",[])
.service("gaoyn_greeter",function(){ //使用service创建一个服务
this.sayHello=function(){
alert("你好factory");
}
this.saybye=function(){
alert("再见");
}
//可以不用return 对象出去,直接使用Service对象--->自动create一个对象出来
})
//使用 angular.module("myApp",[]).controller
.controller("democ",["$scope","gaoyn_greeter",function($scope,gaoyn_greeter){
$scope.hi=function(){
gaoyn_greeter.sayHello();
}
}])
.controller("demoB",function($scope,gaoyn_greeter){
$scope.hello=function(){
gaoyn_greeter.saybye();
}
})
;
</script>
</head>
<body ng-app="myApp" ng-controller="democ">
<button ng-click="hi()">问好</button>
<button ng-controller="demoB" ng-click="hello()">再见</button>
</body>
</html>