{"id":2524,"date":"2017-06-06T20:05:55","date_gmt":"2017-06-06T12:05:55","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=2524"},"modified":"2017-06-06T20:05:55","modified_gmt":"2017-06-06T12:05:55","slug":"angularjs%e7%9a%84factory%e3%80%81service%e5%92%8cprovide%e6%9c%8d%e5%8a%a1%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=2524","title":{"rendered":"AngularJs\u7684factory\u3001service\u548cprovide\u670d\u52a1\u7684\u533a\u522b"},"content":{"rendered":"<p><span style=\"font-size: large;\">\u5f53\u4f60\u521d\u8bd5 Angular \u65f6\uff0c\u5f88\u81ea\u7136\u5730\u5c31\u4f1a\u5f80 controller \u548c scope \u91cc\u5806\u6ee1\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\u3002\u4e00\u5b9a\u8981\u65e9\u70b9\u610f\u8bc6\u5230\uff0ccontroller \u8fd9\u4e00\u5c42\u5e94\u8be5\u5f88\u8584\uff1b\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e94\u7528\u91cc\u5927\u90e8\u5206\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6301\u4e45\u5316\u6570\u636e\u90fd\u5e94\u8be5\u653e\u5728 service \u91cc\u3002\u6211\u6bcf\u5929\u90fd\u4f1a\u5728 Stack Overflow \u4e0a\u770b\u5230\u51e0\u4e2a\u540c\u7c7b\u7684\u95ee\u9898\uff0c\u5173\u4e8e\u5982\u4f55\u5728 controller \u91cc\u4fdd\u5b58\u6301\u4e45\u5316\u6570\u636e\u3002\u8fd9\u5c31\u4e0d\u662f controller \u8be5\u5e72\u7684\u4e8b\u3002\u51fa\u4e8e\u5185\u5b58\u6027\u80fd\u7684\u8003\u8651\uff0ccontroller \u53ea\u5728\u9700\u8981\u7684\u65f6\u5019\u624d\u4f1a\u521d\u59cb\u5316\uff0c\u4e00\u65e6\u4e0d\u9700\u8981\u5c31\u4f1a\u88ab\u629b\u5f03\u3002\u56e0\u6b64\uff0c\u6bcf\u6b21\u5f53\u4f60\u5207\u6362\u6216\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\uff0cAngular \u4f1a\u6e05\u7a7a\u5f53\u524d\u7684 controller\u3002\u4e0e\u6b64\u540c\u65f6\uff0cservice \u53ef\u4ee5\u7528\u6765\u6c38\u4e45\u4fdd\u5b58\u5e94\u7528\u7684\u6570\u636e\uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u5728\u4e0d\u540c\u7684 controller \u4e4b\u95f4\u4f7f\u7528\u3002<\/p>\n<p>Angular \u63d0\u4f9b\u4e863\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u5e76\u6ce8\u518c\u6211\u4eec\u81ea\u5df1\u7684 service\u3002<\/span><\/p>\n<p><b><\/b><\/p>\n<ol>\n<li>1\u3001Factory<\/li>\n<li>2\u3001Service<\/li>\n<li>3\u3001Provider<\/li>\n<\/ol>\n<p><b><\/b><br \/>\n<span style=\"font-size: large;\">1) \u7528 Factory \u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e3a\u5b83\u6dfb\u52a0\u5c5e\u6027\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u5bf9\u8c61\u8fd4\u56de\u51fa\u6765\u3002\u4f60\u628a service \u4f20\u8fdb controller \u4e4b\u540e\uff0c\u5728 controller \u91cc\u8fd9\u4e2a\u5bf9\u8c61\u91cc\u7684\u5c5e\u6027\u5c31\u53ef\u4ee5\u901a\u8fc7 factory \u4f7f\u7528\u4e86\u3002<\/span><\/p>\n<pre>app.controller('myFactoryCtrl', function ($scope, myFactory) {\r\n  $scope.artist = myFactory.getArtist()\r\n})\r\n\r\napp.factory('myFactory', function () {\r\n  var _artist = '';\r\n  var service = {}\r\n\r\n  service.getArtist = function () {\r\n    return _artist\r\n  }\r\n\r\n  return service;\r\n})<\/pre>\n<p>2) Service \u662f\u7528&#8221;new&#8221;\u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u7684\u3002\u56e0\u6b64\uff0c\u4f60\u5e94\u8be5\u7ed9&#8221;this&#8221;\u6dfb\u52a0\u5c5e\u6027\uff0c\u7136\u540e service \u8fd4\u56de&#8221;this&#8221;\u3002\u4f60\u628a service \u4f20\u8fdb controller \u4e4b\u540e\uff0c\u5728controller\u91cc &#8220;this&#8221; \u4e0a\u7684\u5c5e\u6027\u5c31\u53ef\u4ee5\u901a\u8fc7 service \u6765\u4f7f\u7528\u4e86\u3002<\/p>\n<pre>app.controller('myServiceCtrl', function ($scope, myService) {\r\n  $scope.artist = myService.getArtist();\r\n});\r\n\r\napp.service('myService', function () {\r\n  var _artist = '';\r\n  this.getArtist = function () {\r\n    return _artist;\r\n  }\r\n});<\/pre>\n<p>3) Providers \u662f\u552f\u4e00\u4e00\u79cd\u4f60\u53ef\u4ee5\u4f20\u8fdb .config() \u51fd\u6570\u7684 service\u3002\u5f53\u4f60\u60f3\u8981\u5728 service \u5bf9\u8c61\u542f\u7528\u4e4b\u524d\uff0c\u5148\u8fdb\u884c\u6a21\u5757\u8303\u56f4\u7684\u914d\u7f6e\uff0c\u90a3\u5c31\u5e94\u8be5\u7528 provider\u3002<\/p>\n<pre>app.controller('myProviderCtrl', function ($scope, myProvider) {\r\n  $scope.artist = myProvider.getArtist();\r\n  $scope.data.thingFromConfig = myProvider.thingOnConfig;\r\n});\r\n\r\napp.provider('myProvider', function () {\r\n  this._artist = '';\r\n  this.thingFromConfig = '';\r\n\r\n  \/\/Only the properties on the object returned from $get are available in the controller.\r\n  this.$get = function () {\r\n    var that = this;\r\n    return {\r\n      getArtist: function () {\r\n        return that._artist;\r\n      },\r\n      thingonConfig: that.thingFromConfig\r\n    }\r\n  }\r\n});\r\n\r\napp.config(function (myProviderProvider) {\r\n  myProviderProvider.thingFromConfig = 'This was set in config()';\r\n})<\/pre>\n<p><b>\u4e0b\u9762\u4e3a\u8be6\u7ec6\u7684\u89e3\u91ca<\/b><br \/>\n<span style=\"font-size: large;\">\u4e3a\u4e86\u51c6\u786e\u8868\u73b0\u51fa Factory\u3001Service \u548c Provider \u4e4b\u95f4\u7684\u5dee\u522b\uff0c\u4e0b\u9762\u6211\u4eec\u7528 3 \u79cd\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u540c\u4e00\u4e2a\u670d\u52a1\u3002\u8fd9\u4e2a\u670d\u52a1\u4f1a\u7528\u5230 iTunes API \u4ee5\u53ca\u4f7f\u7528 $q \u7684 promise\u3002<\/p>\n<p>1) Factory<\/p>\n<p>Factory \u662f\u521b\u5efa\u548c\u914d\u7f6e\u670d\u52a1\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u9664\u4e86\u201c\u5feb\u901f\u6d4f\u89c8\u201d\u4e4b\u5916\uff0c\u5176\u5b9e\u6ca1\u6709\u4ec0\u4e48\u8981\u8865\u5145\u7684\u3002\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4e3a\u5b83\u6dfb\u52a0\u5c5e\u6027\uff0c\u7136\u540e\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u53ef\u4ee5\u4e86\u3002\u5f53\u4f60\u628a factory \u4f20\u8fdb controller \u4e2d\uff0c\u5bf9\u8c61\u7684\u8fd9\u4e9b\u5c5e\u6027\u5c31\u53ef\u4ee5\u901a\u8fc7 factory \u8bbf\u95ee\u3002\u66f4\u8be6\u7ec6\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u5982\u4e0b\u3002<\/span><\/p>\n<pre>app.factory('myFactory', function () {\r\n  var service = {};\r\n  return service;\r\n});<\/pre>\n<p>\u73b0\u5728\u5982\u679c\u6211\u4eec\u628a&#8221;myFactory&#8221;\u4f20\u8fdb controller \u91cc\uff0c\u9644\u52a0\u5728 &#8220;service&#8221; \u4e0a\u7684\u4efb\u4f55\u5c5e\u6027\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u4e86\u3002<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u5411\u56de\u8c03\u51fd\u6570\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u201cprivate\u201d \u53d8\u91cf\u3002\u5f53\u7136 controller\u4e2d\u662f\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u7684\uff0c\u4e0d\u8fc7\u6211\u4eec\u6700\u7ec8\u8fd8\u662f\u4f1a\u5728\u201cservice\u201d\u4e2d\u8bbe\u7f6esetter\u548c\u4e2agetter \u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5fc5\u8981\u65f6\u4fee\u6539\u8fd9\u4e9b\u201cprivate\u201d\u53d8\u91cf\u3002<\/p>\n<pre>app.factory('myFactory', function ($http, $q) {\r\n  var service = {};\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term=';\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n\r\n  return service;\r\n});<\/pre>\n<p>\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86\uff0c\u6211\u4eec\u6ca1\u6709\u5c06\u53d8\u91cf\/\u51fd\u6570\u52a0\u5230\u201cservice\u201d\u4e2d\u3002\u6211\u4eec\u53ea\u662f\u7b80\u5355\u7684\u521b\u5efa\u4ed6\u4eec\u4ee5\u4fbf\u4e4b\u540e\u7684\u4f7f\u7528\u548c\u4fee\u6539\u3002<\/p>\n<p>1\u3001baseUrl \u662fiTunes API\u8981\u6c42\u7684\u6839URL<\/p>\n<p>\/2\u3001_artist \u662f\u6211\u4eec\u60f3\u8981\u67e5\u627e\u7684\u827a\u672f\u5bb6<\/p>\n<p>3\u3001_finalUrl \u662f\u6700\u7ec8\u7684\u6743\u9650\u5b9aURL\uff0c\u5373\u6211\u4eec\u8c03\u7528iTunes\u7684\u5165\u53e3<br \/>\n\/4\u3001makeUrl \u662f\u4e00\u4e2a\u521b\u5efa\u5e76\u8fd4\u56de\u53cb\u597d\u7684iTunesURL\u7684\u51fd\u6570<\/p>\n<p>\u65e2\u7136\u6211\u4eec\u7684\u5e2e\u624b\/\u79c1\u6709\u53d8\u91cf\u548c\u51fd\u6570\u653e\u5728\u7684\u5408\u9002\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u8ba9\u6211\u4eec\u5411\u201cservice\u201d\u5bf9\u8c61\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u5c5e\u6027\u3002\u65e0\u8bba\u6211\u4eec\u5411\u201dservice\u201c\u4e2d\u6dfb\u52a0\u4ec0\u4e48\uff0c \u6211\u4eec\u90fd\u80fd\u5728\u4efb\u610f\u4e00\u4e2a\u6211\u4eec\u4f20\u9012\u8fdb\u2018myFactory\u2019 \u7684controller\u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u6211\u4eec\u6765\u521b\u5efasetArtist\u548cgetArtist\u65b9\u6cd5\u6765\u7b80\u5355\u7684\u8fd4\u56de\u6216\u8bbe\u7f6eartist\u3002\u540c\u6837\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u4f7f\u7528\u6211\u4eec\u521b\u5efa\u7684URL\u6765\u8c03\u7528iTunes API\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u4eceiTunes \uff21\uff30\uff29\u83b7\u53d6\u6570\u636e\u540e\u4fbf\u4f1a\u6ee1\u8db3\u7684\uff50\uff52\uff4f\uff4d\uff49\uff53\uff45\u3002\u5982\u679c\u4f60\u5bf9\uff21\uff4e\uff47\uff55\uff4c\uff41\uff52\u7684\uff50\uff52\uff4f\uff4d\uff49\uff53\uff45\u63a5\u89e6\u4e0d\u591a\uff0c\u6211\u5f3a\u70c8\u63a8\u8350\u4f60\u6df1\u5165\u7684\u5b66\u4e60\u4e00\u4e0b\u5b83\u3002<\/p>\n<p>setArtist \u63a5\u53d7\u4e00\u4e2a\uff41\uff52\uff54\uff49\uff53\uff54\u5e76\u4e14\u5141\u8bb8\u4f60\u8bbe\u7f6e\uff41\uff52\uff54\uff49\uff53\uff54<\/p>\n<p>getArtist \u8fd4\u56de\uff41\uff52\uff54\uff49\uff53\uff54<\/p>\n<p>callItunes \u9996\u5148\u8c03\u7528makeUrl()\u65b9\u6cd5\u4ee5\u4fbf\u6784\u5efa$http\u8bf7\u6c42\u4f7f\u7528\u7684URL\u3002\u7136\u540e\u5b83\u4f1a\u8bbe\u7f6epromise\u5bf9\u8c61\uff0c\u8ba9$http\u8bf7\u6c42\u6211\u4eec\u6700\u7ec8\u7684URL\uff0c \u518d\u7136\u540e\u5462\uff0c\u56e0\u4e3a$http\u8fd4\u56de\u4e00\u4e2apromise\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5728\u8bf7\u6c42\u540e\u8c03\u7528.success\u6216.error\u3002\u6700\u540e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7iTunes\u7684\u6570\u636e\u6765\u89e3\u6790\u6211\u4eec\u7684promise\uff0c\u6216\u8005\u76f4\u63a5\u2018There was an error\u2019\u6765\u62d2\u7edd\u5b83\u3002<\/p>\n<pre>app.factory('myFactory', function ($http, $q) {\r\n  var service = {};\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term='\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n\r\n  service.setArtist = function (artist) {\r\n    _artist = artist;\r\n  }\r\n\r\n  service.getArtist = function () {\r\n    return _artist;\r\n  }\r\n\r\n  service.callItunes = function () {\r\n    makeUrl()\r\n    var deferred = $q.defer();\r\n    $http({\r\n      method: 'JSONP',\r\n      url: _finalUrl\r\n    }).success(function (data) {\r\n      deferred.resolve(data);\r\n    }).error(function () {\r\n      deferred.reject('There was an error');\r\n    })\r\n\r\n    return deferred.promise;\r\n  }\r\n\r\n  return service;\r\n});<\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u7684factory\u5b8c\u6210\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u5c06&#8221;myFactory&#8221;\u6ce8\u5165\u5230\u4efb\u610fcontroller\u4e2d\u4e86\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8c03\u7528\u6211\u4eec\u6dfb\u52a0\u5230service\u5bf9\u8c61\u4e2d\u7684\u65b9\u6cd5\u4e86\uff08setArtist\uff0cgetArtist\uff0c\u548ccallItunes\uff09\u3002<\/p>\n<pre>app.controller('myFactoryCtrl', function ($scope, myFactory) {\r\n  $scope.data = {};\r\n  $scope.updateArtist = function () {\r\n    myFactory.setArtist($scope.data.artist);\r\n  }\r\n\r\n  $scope.submitArtist = function () {\r\n    myFactory.callItunes()\r\n      .then(function (data) {\r\n        $scope.data.artistData = data;\r\n      }, function (data) {\r\n        alert(data);\r\n      })\r\n  }\r\n})<\/pre>\n<p><span style=\"font-size: large;\">\u5728\u4e0a\u9762\u7684controller\u4e2d\uff0c\u6211\u4eec\u6ce8\u5165\u4e86\u2018myFactory\u2019 service\u5bf9\u8c61\u3002\u7136\u540e\u6211\u4eec\u8bbe\u7f6e$scope \u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u4e0a\u9762\u552f\u4e00\u68d8\u624b\u7684\u4ee3\u7801\u662f\u5904\u7406promise\u3002\u56e0\u4e3acallItunes\u8fd4\u56de\u4e00\u4e2apromise\u5bf9\u8c61\uff0c\u4e00\u65e6\u6211\u4eec\u7684promise\u6ee1\u8db3\u4e86\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528.then()\u65b9\u6cd5\u4ee5\u53ca\u8bbe\u7f6e$scope.data.artistData\u3002\u4f60\u4f1a\u6ce8\u610f\u5230\u6211\u4eec\u7684controller\u662f\u975e\u5e38\u7684\u201c\u7626\u201d\u3002\u56e0\u4e3a\u6211\u4eec\u6240\u6709\u7684\u903b\u8f91\u548c\u6301\u4e45\u5316\u6570\u636e\u90fd\u5b58\u653e\u5728\u4e86service\u4e2d\u800c\u4e0d\u662fcontroller\u4e2d\u3002<\/p>\n<p>2) Service<\/p>\n<p>\u5f53\u6211\u4eec\u521b\u5efa\u4e00\u4e2aService\u65f6\uff0c\u6211\u4eec\u6240\u77e5\u9053\u7684\u6700\u91cd\u8981\u4e8b\u53ef\u80fd\u5c31\u662fService\u901a\u8fc7new\u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u8fd9\u5e94\u8be5\u53ef\u4ee5\u4f7f\u719f\u6089JavaScript\u7684\u4eba\u4e86\u89e3\u5230\u4e86\u8fd9\u6bb5\u4ee3\u7801\u7684\u4f5c\u7528\u3002\u4f46\u5bf9\u4e8e\u90a3\u4e9bJS\u80cc\u666f\u6709\u9650\uff0c\u6216\u8005\u4e0d\u592a\u719f\u6089new\u5173\u952e\u5b57\u7684\u4f5c\u7528\u7684\u4eba\u6765\u8bf4\u53ef\u80fd\u6709\u70b9\u56f0\u96be\u3002\u90a3\u5c31\u8ba9\u6211\u4eec\u6765\u91cd\u6e29\u4e00\u4e0bJavaScript\u7684\u57fa\u672c\u529f\u80fd\uff0c\u4ee5\u4fbf\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3Service\u7a76\u7adf\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u5148\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7new\u5173\u952e\u5b57\u6765\u8c03\u7528\u5b83\uff0c\u770b\u770b\u5f53\u89e3\u91ca\u5668\u9047\u5230\u4e86new\u5173\u952e\u5b57\u7684\u65f6\u5019\u505a\u4e86\u4e9b\u4ec0\u4e48\u5de5\u4f5c\uff0c\u4ee5\u4fbf\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u4f7f\u7528new\u5173\u952e\u5b57\u6765\u5b9e\u4f8b\u5316\u4e00\u4e2a\u51fd\u6570\u65f6\u7a76\u7adf\u6709\u4ec0\u4e48\u53d8\u5316\u3002\u8fd9\u4e2a\u7684\u6700\u7ec8\u7ed3\u679c\u5e94\u8be5\u548cService\u662f\u4e00\u6837\u7684\u3002<\/span><br \/>\n<span style=\"font-size: small;\">\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u6784\u9020\u5668\u3002<\/span><\/p>\n<pre>var Person = function (name, age) {\r\n  this.name = name;\r\n  this.age = age;\r\n}<\/pre>\n<p>\u8fd9\u4e2a\u4e00\u4e2a\u5178\u578b\u7684JavaScript\u5f0f\u7684\u6784\u9020\u65b9\u6cd5\u3002\u73b0\u5728\uff0c\u53ea\u8981\u6211\u4eec\u4f7f\u7528new\u5173\u952e\u5b57\u6765\u8c03\u7528Person\u51fd\u6570\uff0c\u5c31\u4f1a\u5c06&#8217;this&#8217;\u5173\u952e\u5b57\u7ed1\u5b9a\u5230\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u4e0a\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u7ed9Person\u7684prototype\u5bf9\u8c61\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5bf9\u6240\u6709Person \u2018\u7c7b\u2019\u7684\u5b9e\u4f8b\u90fd\u662f\u53ef\u7528\u7684\u3002<\/p>\n<pre>Person.prototype.sayName = function () {\r\n  alert('My name is ' + this.name)\r\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u7531\u4e8e\u6211\u4eec\u5f80prototype\u4e0a\u6dfb\u52a0\u4e86\u4e00\u4e2asayName\u65b9\u6cd5\uff0c\u6240\u4ee5\u6240\u6709\u7684Person\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5e76\u4e14\u8f93\u51fa\u5bf9\u5e94\u5b9e\u4f8b\u7684name\u503c\u3002<br \/>\n\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u4e00\u4e2aPerson\u7684\u6784\u9020\u5668\uff0c\u5e76\u5728\u5728\u5176prototype\u4e0a\u5b9a\u4e49\u4e86\u4e00\u4e2asayName\u65b9\u6cd5\uff0c\u90a3\u5c31\u8ba9\u6211\u4eec\u53bb\u521b\u5efa\u4e00\u4e2aPerson\u7684\u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528\u8fd9\u4e2asayName\u65b9\u6cd5\u3002<\/p>\n<pre>var tyler = new Person('Tyler', 23);\r\ntyler.sayName() \/\/ alerts 'My name is Tyler'<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u628a\u521b\u5efaPerson\u6784\u9020\u5668\u3001\u5f80\u5176prototype\u4e0a\u6dfb\u52a0\u65b9\u6cd5\u3001\u521b\u5efa\u4e00\u4e2aPerson\u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528sayName\u65b9\u6cd5\u7684\u4ee3\u7801\u5199\u5728\u4e00\u5757\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>var Person = function (name, age) {\r\n  this.name = name;\r\n  this.age = age;\r\n}\r\n\r\nPerson.prototype.sayName = function () {\r\n  alert('My name is ' + this.name)\r\n}\r\n\r\nvar tyler = new Person('Tyler', 23);\r\ntyler.sayName() \/\/ alerts 'My name is Tyler'<\/pre>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u5f53\u6211\u4eec\u5728JavaScript\u4e2d\u4f7f\u7528new\u5173\u952e\u5b57\u7684\u65f6\u5019\u7a76\u7adf\u53d1\u751f\u4e86\u4ec0\u4e48\u3002\u9996\u5148\u4f60\u5e94\u8be5\u5df2\u7ecf\u6ce8\u610f\u5230\u7684\u662f\uff0c\u5f53\u6211\u4eec\u5728\u4f8b\u5b50\u4e2d\u4f7f\u7528\u4e86new\u5173\u952e\u5b57\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7&#8217;tyler&#8217;\u6765\u8c03\u7528\u65b9\u6cd5\uff08sayName\uff09\uff0c\u770b\u4e0a\u53bb\u597d\u50cftyler\u662f\u4e00\u4e2a\u5bf9\u8c61\u2014\u2014\u90a3\u662f\u56e0\u4e3a\u5b83\u786e\u5b9e\u6210\u4e86\u4e00\u4e2a\u5bf9\u8c61\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u77e5\u9053\u7684\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u6211\u4eec\u7684Person\u6784\u9020\u5668\u8fd4\u56de\u4e86\u4e00\u4e2a\u5bf9\u8c61(object)\u3002\u5176\u6b21\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u7531\u4e8e\u6211\u4eec\u7684sayName\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728Person\u7684prototype\u4e0a\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5b9a\u4e49\u5728Person\u7684\u5b9e\u4f8b\u4e0a\u7684\uff0c\u6240\u4ee5Person\u51fd\u6570\u8fd4\u56de\u7684\u5bf9\u8c61\uff08tyler\uff09\u4e00\u5b9a\u662f\u7531\u4e8e\u672a\u627e\u5230sayName\u65b9\u6cd5\uff0c\u8fdb\u800c\u53bbprototype\u5bfb\u627esayName\u65b9\u6cd5\u7684\u3002\u7528\u66f4\u901a\u4fd7\u7684\u8bdd\u6765\u8bf4\uff0c\u5f53\u6211\u4eec\u8c03\u7528tyler.sayName()\u65f6\uff0cJS\u89e3\u91ca\u5668\u8bf4\uff0c\u201c\u597d\u5427\uff0c\u6211\u5148\u53bb\u6211\u4eec\u521a\u521b\u5efa\u7684&#8217;tyler&#8217;\u5bf9\u8c61\u4e0a\u67e5\u627esayName\u65b9\u6cd5\uff0c\u7136\u540e\u8c03\u7528\u5b83\u3002\u7b49\u4e00\u4e0b\uff0c\u6211\u6ca1\u6709\u5728\u5b83\u4e0a\u9762\u627e\u5230sayName\u65b9\u6cd5\u2014\u2014\u6211\u53ea\u770b\u5230\u4e86name\u548cage\uff0c\u90a3\u8ba9\u6211\u53bbprototype\u627e\u4e00\u4e0b\u5427\u3002\u6ca1\u9519\uff0c\u5b83\u5728prototype\u4e0a\uff0c\u90a3\u5c31\u8ba9\u6211\u8c03\u7528\u5b83\u5427\u201d\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5728JavaScript\u4e2d\u4f7f\u7528new\u5173\u952e\u4e4b\u540e\u6240\u505a\u7684\u4e8b\u3002\u5b83\u662f\u4e0a\u9762\u8fd9\u4e00\u6bb5\u6587\u5b57\u7684\u4e00\u4e2a\u57fa\u672c\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u6211\u5df2\u7ecf\u628a\u4eceJS\u89e3\u91ca\u5668\u7684\u89d2\u5ea6\u6765\u770b\u6574\u4e2a\u8fc7\u7a0b\u7684\u4ee3\u7801\u5199\u5728\u4e86\u6ce8\u91ca\u91cc\u3002<\/p>\n<pre>var Person = function (name, age) {\r\n  \/\/ The next line creates an object that will\r\n  \/\/ delegate to the person's prototype on failed lookups.\r\n\r\n  \/\/var obj = Object.create(Person.prototype);\r\n\r\n  \/\/ The next line sets 'this' to the newly created object\r\n  \/\/ this = obj;\r\n\r\n  this.name = name;\r\n  this.age = age;\r\n\r\n  \/\/ return this;\r\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u65e2\u7136\u6211\u4eec\u4e86\u89e3\u4e86\u5728JavaScript\u4e2dnew\u5173\u952e\u5b57\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u90a3\u4e48\u5728Angular\u4e2d\u521b\u5efa\u4e00\u4e2aService\u4e5f\u5e94\u8be5\u53d8\u5f97\u5bb9\u6613\u7406\u89e3\u4e86\u3002<\/p>\n<p>\u5728\u521b\u5efa\u4e00\u4e2aService\u65f6\uff0c\u9700\u8981\u7406\u89e3\u7684\u6700\u91cd\u8981\u7684\u4e00\u4ef6\u4e8b\u5c31\u662f\u6211\u4eec\u4f7f\u7528new\u5173\u952e\u5b57\u53bb\u5b9e\u4f8b\u5316Service\u5bf9\u8c61\u3002\u7ed3\u5408\u6211\u4eec\u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u6240\u4e86\u89e3\u5230\u7684\u77e5\u8bc6\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u610f\u8bc6\u5230\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u76f4\u63a5\u6dfb\u52a0\u5230this\u4e0a\uff0c\u4e4b\u540e\uff0c\u5728\u521b\u5efaService\u5bf9\u8c61\u65f6\uff0cthis\u4f1a\u88ab\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u3002\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u8fd9\u79cd\u5de5\u4f5c\u65b9\u5f0f\u3002<\/p>\n<p>\u6211\u4eec\u4e0d\u7528\u50cf\u4e4b\u524dFactory\u4e2d\u7684\u4f8b\u5b50\u90a3\u6837\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\u3002\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u4e86new\u5173\u952e\u5b57\u6765\u8c03\u7528\uff0c\u89e3\u91ca\u5668\u4f1a\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u5173\u8054\u5b83\u7684prototype\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u8be5\u5bf9\u8c61\u8fd4\u56de\uff0c\u800c\u4e0d\u7528\u6211\u4eec\u53bb\u505a\u8fd9\u4e9b\u5de5\u4f5c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u6211\u4eec\u7684\u79c1\u6709\u8f85\u52a9\u51fd\u6570\u3002\u5b83\u5e94\u8be5\u770b\u8d77\u6765\u548c\u6211\u4eec\u5728factory\u4e2d\u6240\u4f5c\u7684\u5de5\u4f5c\u5f88\u7c7b\u4f3c\u3002\u7531\u4e8e\u6211\u5df2\u7ecf\u5728factory\u7684\u4f8b\u5b50\u4e2d\u89e3\u91ca\u8fc7\u6bcf\u4e00\u884c\u4ee3\u7801\u7684\u542b\u4e49\u4e86\uff0c\u6240\u4ee5\u6211\u4e0d\u4f1a\u5728\u8fd9\u91cc\u591a\u4f5c\u89e3\u91ca\uff0c\u5982\u6709\u7591\u60d1\uff0c\u8bf7\u518d\u6b21\u56de\u5473\u4e00\u4e0bfactory\u7684\u4f8b\u5b50\u3002<\/p>\n<pre>app.service('myService', function ($http, $q) {\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term=';\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n});<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8981\u628a\u53ef\u4ee5\u4ece\u63a7\u5236\u5668\u4e2d\u8bbf\u95ee\u7684\u65b9\u6cd5\u6dfb\u52a0\u5230\u2018this\u2019\u4e0a\u3002<\/p>\n<pre>app.service('myService', function ($http, $q) {\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term=';\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n\r\n  this.setArtist = function (artist) {\r\n    _artist = artist;\r\n  }\r\n\r\n  this.getArtist = function () {\r\n    return _artist;\r\n  }\r\n\r\n  this.callItunes = function () {\r\n    makeUrl()\r\n    var deferred = $q.defer();\r\n    $http({\r\n      method: 'JSONP',\r\n      url: _finalUrl\r\n    }).success(function (data) {\r\n      deferred.resolve(data);\r\n    }).error(function () {\r\n      deferred.reject('There was an error');\r\n    })\r\n\r\n    return deferred.promise;\r\n  }\r\n\r\n});<\/pre>\n<p>\u73b0\u5728\uff0c\u548c\u4f7f\u7528factory\u4e00\u6837\uff0c\u6240\u6709\u5c06myService\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u7684\u63a7\u5236\u5668\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230setArtist, getArtist, \u548ccallItunes\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4f20\u5165\u4e86myService\u7684\u63a7\u5236\u5668\uff08\u57fa\u672c\u4e0a\u548cfactory\u7684\u63a7\u5236\u5668\u4e00\u6837\uff09\u3002<\/p>\n<pre>app.controller('myService', function ($scope, myService) {\r\n  $scope.data = {};\r\n  $scope.updateArtist = function () {\r\n    myService.setArtist($scope.data.artist);\r\n  }\r\n\r\n  $scope.submitArtist = function () {\r\n    myService.callItunes()\r\n      .then(function (data) {\r\n        $scope.data.artistData = data;\r\n      }, function (data) {\r\n        alert(data);\r\n      })\r\n  }\r\n});<\/pre>\n<p>\u6b63\u5982\u6211\u4e4b\u524d\u63d0\u5230\u7684\u90a3\u6837\uff0c\u4e00\u65e6\u4f60\u4e86\u89e3\u4e86new\u5173\u952e\u5b57\u7684\u4f5c\u7528\uff0c\u4f60\u5c31\u4f1a\u77e5\u9053\u5728Angular\u4e2d\uff0cServices\u548cFactories\u51e0\u4e4e\u4e00\u6837\u3002<\/p>\n<p>3) Provider<\/p>\n<p>\u8981\u8bb0\u4f4f\u7684\u5173\u4e8eProvider\u7684\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u662f\uff0c\u5b83\u4eec\u662f\u4f60\u53ef\u4ee5\u4f20\u9012\u5230\u5e94\u7528\u7a0b\u5e8f\u7684app.config\u90e8\u5206\u552f\u4e00\u7684\u670d\u52a1\u3002\u5982\u679c\u4f60\u9700\u8981\u5728\u4f60\u7684\u670d\u52a1\u5bf9\u8c61\u53ef\u4ee5\u5728\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u5916\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u7528\u4e4b\u524d\u6539\u53d8\u5b83\u7684\u67d0\u4e9b\u90e8\u5206\uff0c\u8fd9\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u867d\u7136Services\/Factories\u5f88\u76f8\u4f3c\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5dee\u5f02\uff0c\u6211\u4eec\u5c06\u4f1a\u8ba8\u8bba\u5b83\u4eec\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u7528\u4e0e\u6211\u4eec\u5efa\u7acbService \u548c Factory\u7c7b\u4f3c\u7684\u65b9\u5f0f\u6765\u5efa\u7acb\u6211\u4eec\u7684Provider\u3002\u4e0b\u9762\u7684\u53d8\u91cf\u662f\u6211\u4eec\u7684&#8217;\u79c1\u4eba&#8217;\u548c\u8f85\u52a9\u529f\u80fd\u3002<\/p>\n<pre>app.provider('myProvider', function ($http, $q) {\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term=';\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  \/\/ Going to set this property on the config fn below\r\n  this.thingFromConfig = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n});<\/pre>\n<p>\u540c\u6837\u5730\uff0c\u5982\u679c\u4e0a\u9762\u7684\u4ee3\u7801\u7684\u4efb\u4f55\u90e8\u5206\u4ee4\u4f60\u7ea0\u7ed3\uff0c\u8bf7\u770b\u4e0b Factory \u90e8\u5206\uff0c\u5728\u90a3\u91cc\u6211\u66f4\u8be6\u7ec6\u5730\u89e3\u91ca\u4e86\u8fd9\u4e9b\u4ee3\u7801\u7684\u4f5c\u7528\u3002<br \/>\n\u5fc5\u987b\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\u53ea\u6709\u8fd9\u4e9b\u53d8\u91cf\u548c\u51fd\u6570\u662f\u53ef\u4ee5\u5728\u6211\u4eec\u7684app.config\u51fd\u6570\u4e2d\u8bbf\u95ee\u7684\u3002\u8fd9\u66fe\u4e00\u5ea6\u4f7f\u6211\u611f\u5230\u56f0\u60d1\uff0c\u6240\u4ee5\u4f60\u6700\u597d\u4e5f\u8981\u77e5\u9053\u8fd9\u70b9\u4e0d\u540c\u4e4b\u5904\u3002\u4f60\u53ef\u4ee5\u628aProvider\u60f3\u8c61\u6210\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u7b2c\u4e00\u90e8\u5206\u7684\u53d8\u91cf\u548c\u51fd\u6570\u662f\u53ef\u4ee5\u5728app.config\u51fd\u6570\u4e2d\u8bbf\u95ee\u7684\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u5b83\u4eec\u88ab\u5176\u4ed6\u5730\u65b9\u8bbf\u95ee\u5230\u4e4b\u524d\u6765\u4fee\u6539\u5b83\u4eec\uff08\u5982\u4e0a\u6240\u793a\uff09\u3002\u7b2c\u4e8c\u90e8\u5206\uff08\u5982\u4e0b\u6240\u793a\uff09 \u7684\u53d8\u91cf\u548c\u51fd\u6570\u662f\u53ef\u4ee5\u5728\u4efb\u4f55\u4f20\u5165\u4e86\u2019myProvider\u2018\u7684\u63a7\u5236\u5668\u4e2d\u8fdb\u884c\u8bbf\u95ee\u7684\u3002<\/p>\n<p>\u5f53\u4f60\u4f7f\u7528Provider\u521b\u5efa\u4e00\u4e2aservice\u65f6\uff0c\u552f\u4e00\u7684\u53ef\u4ee5\u5728\u4f60\u7684\u63a7\u5236\u5668\u4e2d\u8bbf\u95ee\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u901a\u8fc7$get()\u51fd\u6570\u8fd4\u56de\u5185\u5bb9\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c06$get\u65b9\u6cd5\u5199\u5728\u4e86\u2019this\u2018\uff08\u6700\u7ec8\u4f1a\u88ab\u51fd\u6570\u8fd4\u56de\uff09\u4e0a\u3002\u73b0\u5728\uff0c$get\u51fd\u6570\u4f1a\u8fd4\u56de\u6240\u6709\u6211\u4eec\u5e0c\u671b\u5728\u63a7\u5236\u5668\u4e2d\u8fdb\u884c\u8bbf\u95ee\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>this.$get = function ($http, $q) {\r\n  return {\r\n    callItunes: function () {\r\n      makeUrl()\r\n      var deferred = $q.defer();\r\n      $http({\r\n        method: 'JSONP',\r\n        url: _finalUrl\r\n      }).success(function (data) {\r\n        deferred.resolve(data);\r\n      }).error(function () {\r\n        deferred.reject('There was an error');\r\n      })\r\n\r\n      return deferred.promise;\r\n    },\r\n    setArtist: function (artist) {\r\n      _artist = artist;\r\n    },\r\n    getArtist = function () {\r\n      return _artist;\r\n    },\r\n    thingonConfig: this.thingFomConfig\r\n  }\r\n}<\/pre>\n<p>\u73b0\u5728\uff0cProvider\u7684\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>app.provider('myProvider', function ($http, $q) {\r\n  var baseUrl = 'https:\/\/itunes.apple.com\/search?term=';\r\n  var _artist = '';\r\n  var _finalUrl = '';\r\n\r\n  \/\/ Going to set this property on the config fn below\r\n  this.thingFromConfig = '';\r\n\r\n  var makeUrl = function () {\r\n    _artist = _artist.split(' ').join('+');\r\n    _finalUrl = baseUrl + _artist + '&amp;callback=JSON_CALLBACK';\r\n    return _finalUrl;\r\n  }\r\n\r\n  this.$get = function ($http, $q) {\r\n    return {\r\n      callItunes: function () {\r\n        makeUrl()\r\n        var deferred = $q.defer();\r\n        $http({\r\n          method: 'JSONP',\r\n          url: _finalUrl\r\n        }).success(function (data) {\r\n          deferred.resolve(data);\r\n        }).error(function () {\r\n          deferred.reject('There was an error');\r\n        })\r\n\r\n        return deferred.promise;\r\n      },\r\n      setArtist: function (artist) {\r\n        _artist = artist;\r\n      },\r\n      getArtist = function () {\r\n        return _artist;\r\n      },\r\n      thingonConfig: this.thingFomConfig\r\n    }\r\n  }\r\n});<\/pre>\n<p>\u73b0\u5728\uff0c\u4e0e\u6211\u4eec\u7684Factory\u548cService\u7c7b\u4f3c\uff0csetArtist, getArtist, \u548ccallItunes\u53ef\u4ee5\u5728\u4efb\u4f55\u4e00\u4e2a\u4f20\u5165\u4e86 myProvider \u7684\u63a7\u5236\u5668\u4e2d\u8bbf\u95ee\u3002\u4e0b\u9762\u662fmyProvider\u7684\u63a7\u5236\u5668\uff08\u51e0\u4e4e\u548c\u6211\u4eecFactory\/Service\u4e2d\u7684\u63a7\u5236\u5668\u4e00\u6837\uff09\u3002<\/p>\n<pre>app.controller('myProviderCtrl', function ($scope, myProvider) {\r\n  $scope.data = {};\r\n  $scope.updateArtist = function () {\r\n    myProvider.setArtist($scope.data.artist);\r\n  }\r\n\r\n  $scope.submitArtist = function () {\r\n    myProvider.callItunes()\r\n      .then(function (data) {\r\n        $scope.data.artistData = data;\r\n      }, function (data) {\r\n        alert(data);\r\n      })\r\n  }\r\n\r\n  $scope.data.thingFromConfig = myProvider.thingOnConfig;\r\n});<\/pre>\n<p>\u6b63\u5982\u524d\u9762\u63d0\u5230\u7684\u90a3\u6837\uff0c\u4f7f\u7528Provider\u521b\u5efa\u4e00\u4e2aservice\u7684\u72ec\u7279\u4e4b\u5904\u662f\uff0c\u4f60\u53ef\u4ee5\u5728Provider\u5bf9\u8c61\u4f20\u9012\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u4e4b\u524d\u5728app.config\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5bf9\u5e94\u7684\u4f8b\u5b50\u3002<\/p>\n<pre>app.config(function (myProviderProvider) {\r\n  \/\/ Note that NG appends 'Provider' to the end of the provider name.\r\n  myProviderProvider.thingFromConfig = 'This sentence was set in app.config. Providers are the only service that can be passed into config. Check out the code to see how it works.';\r\n})<\/pre>\n<p>\u73b0\u5728\u4f60\u53ef\u4ee5\u660e\u767d\u2018thingFromConfig\u2019\u662f\u600e\u4e48\u6837\u5730\u5728\u6211\u4eec\u7684provider\u4e2d\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u800c\u5f53\u5b83\u51fa\u73b0\u5728DOM\u4e2d\u65f6\uff0c\u5b83\u5c06\u662f&#8217;This sentence was set\u2026\u2019\u3002<\/p>\n<p>\u8c22\u8c22\u60a8\u7684\u9605\u8bfb\uff0c\u6211\u5e0c\u671b\u8fd9\u6709\u52a9\u4e8e\u4f60\u80fd\u8fa8\u522b\u5728Angular\u4e2dFactory, Service, \u548c Provider\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<p>\u8981\u67e5\u770b\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1ahttps:\/\/github.com\/tylermcginnis33\/AngularService<\/p>\n<p>&nbsp;<\/p>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=2524\">AngularJs\u7684factory\u3001service\u548cprovide\u670d\u52a1\u7684\u533a\u522b<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u521d\u8bd5 Angular \u65f6\uff0c\u5f88\u81ea\u7136\u5730\u5c31\u4f1a\u5f80 controller \u548c scope \u91cc\u5806\u6ee1\u4e0d\u5fc5\u8981\u7684\u903b\u8f91\u3002\u4e00\u5b9a\u8981\u65e9\u70b9\u610f\u8bc6\u5230\uff0ccontroller \u8fd9\u4e00\u5c42\u5e94\u8be5\u5f88\u8584\uff1b\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e94\u7528\u91cc\u5927\u90e8\u5206\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u6301\u4e45\u5316 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[186],"tags":[53,364,366,365,98],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2524"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2524"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2524\/revisions"}],"predecessor-version":[{"id":2525,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/2524\/revisions\/2525"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}