<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>index_provider_service_factory的实例</title>
<script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div ng-app="firstApp">
<div ng-controller="firstController">
<li>factory name:{{factoryName}}</li>
<br><li>service name:{{serviceName}}</li>
<br><li>provider name:{{providerName}}</li>
<br>provider 可以在应用启动时进行配置:
<br><li>config to provider:{{provideAge}}</li>
</div>
</div>
<script type="text/javascript"> var firstApp=angular.module('firstApp',[]); firstApp.factory('firstFactory',function(){ var firstFactory={}; firstFactory.name="first factory Name"; return firstFactory; }); firstApp.service('firstService',function() { this.nameService = 'first service'; }); firstApp.provider('firstProvider',function() { var ageDefault=20; var age=ageDefault; this.setAge=function(ageNew){ age=ageNew; } this.$get = function() { var result = {}; result.providerName = 'name from provider'; result.age=age; result.defaultAge=ageDefault; return result; }}); firstApp.controller('firstController',['$scope','firstFactory','firstService','firstProvider',function($scope,firstFactory,firstService,firstProvider) { $scope.factoryName =firstFactory.name; $scope.serviceName =firstService.nameService; $scope.providerName = firstProvider.providerName; $scope.provideAge=firstProvider.age; }]); firstApp.config(function(firstProviderProvider) { fir> tProviderProvider.setAge(26); }); </script>
</body>
</html>