自定义服务:
方法一: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