angularjs-factory,provider,service,constant,value,decorator

來源:互聯網
上載者:User

標籤:service   factory   decorator   value   provider   

1、factory

用 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>


angularjs-factory,provider,service,constant,value,decorator

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.