angularjs-factory,provider,service,constant,value,decorator,route
2017-06-28 23:40
393 查看
1、factory
用 Factory 就是创建一个对象,为它添加属性,然后把这个对象返回出来。你把 service 传进 controller 之后,在 controller 里这个对象里的属性就可以通过 factory 使用了。
Providers 是唯一一种你可以传进 .config() 函数的 service。当你想要在 service 对象启用之前,先进行模块范围的配置,那就应该用 provider。
Service 是用"new"关键字实例化的。因此,你应该给"this"添加属性,然后 service 返回"this"。你把 service 传进 controller 之后,在controller里 "this" 上的属性就可以通过 service 来使用了。
constant不可修饰
需要http服务器
用 Factory 就是创建一个对象,为它添加属性,然后把这个对象返回出来。你把 service 传进 controller 之后,在 controller 里这个对象里的属性就可以通过 factory 使用了。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <script> angular.module('mod', []) //定义工厂模块-factory .factory('fact', [function () { return { str:"testfactory", sum:function (n1,n2) { return n1+n2 } }; }]) //添加依赖注入模块fact .controller('testCtrl', ['$scope','fact', function ($scope,fact) { alert(fact.str) }]) </script> </head> <body ng-app='mod' ng-controller='testCtrl'> </body> </html>2、provide
Providers 是唯一一种你可以传进 .config() 函数的 service。当你想要在 service 对象启用之前,先进行模块范围的配置,那就应该用 provider。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <script> angular.module('mod', []) .controller('modCtrl', ['$scope','prod', function ($scope,prod) { alert(prod.a+prod.b) }]) .provider('prod', [function () { this.$get = [function() { return { a:12, b:15 }; }]; }]) </script> </head> <body ng-app='mod' ng-controller='modCtrl'> </body> </html>3、service
Service 是用"new"关键字实例化的。因此,你应该给"this"添加属性,然后 service 返回"this"。你把 service 传进 controller 之后,在controller里 "this" 上的属性就可以通过 service 来使用了。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <script> angular.module('mod', []) .service('serv', [function () { this.a=12 }]) .controller('modCtrl', ['$scope','serv', function ($scope,serv) { alert(serv.a) }]) </script> </head> <body ng-app='mod' ng-controller='modCtrl'> </body> </html>4、constant与value
constant不可修饰
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <script> angular.module('mod', []) .constant('VERSION', '5.0.3') .value('name', 'cisco') .controller('modCtrl', ['$scope','VERSION','name', function ($scope,VERSION,name) { alert(name+':'+VERSION) }]) </script> </head> <body ng-app='mod' ng-controller='modCtrl'> </body> </html>5、decorator
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <script> angular.module('mod', []) .constant('VERSION', '5.0.3') .value('name', 'cisco') .controller('modCtrl', ['$scope','VERSION','name','prod', function ($scope,VERSION,name,prod) { alert(name+' '+prod.nxos+''+prod.type+' '+prod.date+' '+VERSION) }]) .provider('prod', [function () { this.$get = [function() { return { nxos:'nxos', type:'5548' }; }]; }]) .decorator('prod',function ($delegate) { $delegate.date='2007.1.10' return $delegate }) </script> </head> <body ng-app='mod' ng-controller='modCtrl'> </body> </html>6、route
需要http服务器
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="angular.min.js"></script> <!-- 引入angular-route文件 --> <script src="angular-route.min.js"></script> <script> //引入ngRoute模块 angular.module('mod', ['ngRoute']) .controller('routeCtrl', ['$scope','$route', function ($scope,$route) { }]) .controller('gameCtrl', ['$scope', function ($scope) { $scope.name='cxiong' $scope.scope='9999' }]) //配置route的$routeProvider .config(['$routeProvider', function ($routeProvider) { $routeProvider .when('/game', { templateUrl: 'game.html', controller:'gameCtrl' }) .when('/sport', { templateUrl: '../index.html' }) .when('/news', { templateUrl: '../style.html' }) }]) </script> </head> <body ng-app='mod' ng-controller='routeCtrl'> <a href="#/game">游戏竞技</a> <a href="#/sport">劲爆体育</a> <a href="#/news">全球新闻</a> <ng-view></ng-view> </body> </html>
相关文章推荐
- [转]angularjs的provider~ (Provider, Value, Constant, Service, Factory, Decorator)
- AngularJS之Provider, Value, Constant, Service, Factory, Decorator的区别与详解
- AngularJS的Provider, Value, Constant, Service, Factory, Decorator的区别与详解
- [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)(转)
- 【AngularJS系列4】Provider, Value, Constant, Service, Factory, Decorator
- [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)
- [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)
- AngularJS的Provider, Value, Constant, Service, Factory, Decorator的区别与详解
- AngularJS的Provider, Value, Constant, Service, Factory, Decorator的区别与详解
- AngularJS 1.x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)
- AngularJS的自定义服务,factory、provider、service、constant、value等方法的区别
- Angularjs1.x 中的 service,factory,provider,constant,value
- 简话Angular 07 Angular config-run-service-factory-provider-constant-value
- AngularJS之Factory vs Service vs Provider
- 深究AngularJS——自定义服务详解(factory、service、provider)
- Angularjs 自定义服务 provide 里 provider 方法 以及 factory、 service 方法以及 provider 供应商的概念
- angularjs factory,service,provider 自定义服务的不同
- 深究AngularJS——自定义服务详解(factory、service、provider)
- AngularJS服务之 Factory vs Service vs Provider
- [转载]AngularJS之Factory vs Service vs Provider