https://meilishiyan-song.taobao.com/

angularJs之service

      

自定义服务:

方法一:controller中返回值,service中return

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular. min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">

<p>000000000</p>

<h1>{{hex}}</h1>

</div>

<p>自定义服务:</p>

<script>
var app = angular.module('myApp', []);

app.service('hexafy', function() {
this.myFunc = function (x) {
return 56;
}
});
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy .myFunc(255);

});
</script>

</body>
</html>

 

 方法一:controller中无返回值,service中无return,直接将值存入$scope

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular. min.js"></script> 
</head> 
<body> 
<div ng-app="myApp" ng-controller="myCtrl">

<p>000000000</p>

<h1>{{hex}}</h1>

</div>

<p>自定义服务:</p>

<script> 
var app = angular.module('myApp', []);app.service('hexafy', function() { 

this.myFunc = function (scope,x) { 
scope.hex =56; 

}); 
app.controller('myCtrl', function($scope, hexafy) { 
 hexafy .myFunc($scope,255); 

}); 
</script>

</body> 
</html>

不需要去new service

 

posted @ 2016-12-29 10:14  望梦圆  阅读(119)  评论(0编辑  收藏  举报
https://meilishiyan-song.taobao.com/