我有以下示例,显示了从工厂和工厂获得的结果。一项服务。据我所知,工厂总是 返回一个对象 ,而服务总是 返回一个对象的实例 。在这里,概念上2个返回对象的区别是什么。有人可以澄清吗? (特别针对这种情况)
段:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<script>
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl', function($scope, myFactory, myService){
$scope.name = "Peter";
var a = myService;
console.log(a);
var b = myFactory;
console.log(b);
});
//services declaration
app.service('myService',function(){
this.age = 15;
});
app.factory('myFactory',function(){
var obj = {};
obj.country = "USA";
return obj;
});
</script>
</body>
</html>
结果:
已经阅读: