1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>Provider</title> 11 <script type="text/javascript" src="~/Scripts/angular.js"></script> 12 </head> 13 <body> 14 <div ng-app="myApp"> 15 <div ng-controller="firstController"> 16 {{name}} 17 </div> 18 </div> 19 <script type="text/javascript"> 20 var myApp = angular.module("myApp", []); 21 myApp.config(function ($provide) { 22 $provide.provider(‘providerServices01‘, function () {//自定义服务,通过config来自定义服务 23 this.$get = function () { 24 return { 25 message: ‘this is providerServices01‘ 26 } 27 } 28 }); 29 30 $provide.provider(‘providerServices02‘, function () {//自定义服务,通过config来自定义服务 31 this.$get = function () { 32 var _name = ""; 33 var service = {}; 34 service.setName = function (name) { 35 _name = name; 36 } 37 service.getName = function (name) { 38 return _name; 39 } 40 return service; 41 } 42 }); 43 44 }); 45 myApp.controller("firstController", ["$scope", "providerServices01", "providerServices02", function ($scope, providerServices01, providerServices02) { 46 providerServices02.setName("李四"); 47 $scope.name = providerServices02.getName(); 48 }]); 49 50 51 </script> 52 53 </body> 54 </html>
时间: 2024-10-12 23:41:58